var state = 0;//0=加载，1=主界面，2=游戏界面，3=game over，4=pause，5=游戏指导界面, 6=人物选择，7=道具购买, 8=分数结算界面, 9=战机升级  10=宠物界面   11=暗黑世界  12=关卡界面    13=空白
var backState;
var effect_count = 0;
var SW = 0;
var SH = 0;
var jsGameScreen;
var canvas;
var circleRoundXYArray;//圆形边  24个点与圆心的距离
var CoinBtnStartX = 187;
var DiamondBtnStartX = 451;

//ui
var toast4JS;
var lottoScreen;
var alertDialog;
var img_dialoBg;
var loading4JS;
var img_loading;
var chargeDialog;
var img_charge_back;
var img_charge_barbg;
var img_charge_btn_coin;
var img_charge_btn_diamond;
var img_charge_font_b;
var img_charge_ic_coin;
var img_charge_ic_diamond;
var img_charge_next;
var img_charge_title;
var img_charge_upper;
var img_charge_x;

var img_lotto_bg;
var img_lotto_bg_light;
var img_lotto_bg_round;
var img_lotto_btn_cost;
var img_lotto_btn_free;
var imgs_lotto_light;
var img_lotto_npc;


//loading
var img_startpage_icon;
//var img_startpage_font1;
var img_startpage_font2;
var img_startpage_star;
var img_startpage_lower;
var img_startpage_upper;

var loadCount = 0;
var loadTotal = 0;
var loadGameOK = false;
var loadState = 0;
//var load_drawStartX;
//var load_basecolorSum;
//var load_drawStartY;
//var load_colorArray;
var load_banjing = 50;
var load_sradius = 5;
var load_currentIndex = -1;
var load_baseIndex = 0;
//logo界面
var logo_bg;
var logo_name;
var logo_opera;
var black_cloud;
var cloudMoveCount = 0;
//人物选择界面
var BuyDriverCostArray = [0,10,20,50];
var fightDriverIndex = 0;
var driverIndex = 0;
var select_bg;
var imgs_driver_b;//驾驶员大头像
//var imgs_driver_n;//驾驶员名字
var img_select_choose;
var img_select_buy_d;
var img_select_kick;//点击升级战机
var img_num_small;
var img_planename;
var img_planestar;
var img_select_dice;
var img_select_anim;
var img_select_rank;
var img_select_store;
var img_select_daily;
var myPlaneLevel = [1,0,0,0];//战机等级
var myMaxScore = 1000;//历史最高分
var myFightAttack = [5000,5000,5000,5000];//战斗力
var hasChooseNameArray = [false,false,false,false];//是否选择了驾驶员名字
var driverName = ["","","",""];
var driverNameIndex = [];
var DriverNameArray;
//暗黑世界
var img_blackhole_bg;
var img_btn_start_yes;
var currentBlackStage = 1;//当前暗黑世界层数
var isBlackStateUnlock = false;//当前层是否解锁
var unlockBlackNeedKeys;//解锁所需的钥匙
var unlockBlackNeedCoins;//解锁所需的金币
//关卡界面
var MissionPosXY=[[43,647],[128,619],[210,591],[291,563],[366,524],[291,468],[210,440],[128,399],[43,361],[128,305],[210,261],[291,233],[366,205],[291,136],[210,80]];
var MissionUnlock = [true,false,false,false,false,false,false,false,false,false,false,false,false,false,false];
var startMissionIndex;
var MissionSelectIndex = 0;
var img_mission_bg;
var img_mission_select;
var img_mission_star;
var img_mission_star_u;
//战机升级
var img_btn_back;
var img_p_levelup_bg;
var img_btn_strang_yes;
var img_btn_strang_no;
var img_p_levelup_move;
var img_num_levelup;
var p_levelup_move = 0;
var showLevelupMove = false;
var pLevelUpCostCoin;//升级所需的金币
var pLevelUpCostDiamond;//升级所需的金币
var pLevelUpCostMateria;//升级所需的残骸
//宠物界面
var img_anim_center_bg;
//var imgs_driver_sn;
var img_animname_bg;
var imgs_anim_b;
var img_font_cur_material;
var imgs_anim_egg;
var img_font_unlock;
var img_anim_lock;
var img_btn_lock_yes;
var img_btn_lock_no;
var img_effect_light;
var animSelectIndex = 0;
var animName = ["绿藻宝贝","螳螂战士","璐璐之心","黄金瓢虫","铁甲钢虫","无敌萌宠"];
var animSkillName = ["穿甲弹","爆裂弹","闪电激光","爱心大炮","烽火燎原","聚能浮游炮"];
var animLevelArray = [1,0,0,0,0,0];//宠物等级数组
var animAttackArray = [1,0,0,0,0,0];//宠物攻击数组
var animLockCostCoin;
var animLockCostDiamond;
var changeAngle = 0;
var showLightEffect = false;
//金币和钻石
var myCoin = 100000;
var myDiamond = 10000;

var myMaterialArray = [0,10,100,200,300,400];

var img_coin_pre;
var img_coin_add;
var img_diamond_pre;
var img_diamond_add;
var img_num_money;
//道具购买界面
var imgs_driver_s;
var tool_bg;
var myToolArray = [0, 0, 0, 0];
var ToolCostArray = [400, 10, 10, 50];


//游戏界面
var img_btntools;
var img_btnPause;
var img_btnNoSound;
var img_btnSound;
var img_font_gamewin;
var black_bg;
var red_bg;
var changingAlphaCount = 0;
var isChangingAlphaCountIncrease = false;
var changingMap = false;
var currentMapIndex = 0;
var game_bg_array;
var white_cloud;
var scoreImg;
var font_hp;
var font_lv;
var mapMoveCount = 0;
var mapMoveTotal = 0;
var nextArrivalDis = 100;
var arrivalIndex = 0;//当前第几波
var defeatEnermyCount = 0;
var planesArray = new Array();//飞机波数组
var bulletArray = new Array();//敌人子弹数组
//var BulletArrayTotal = 100;//子弹上限
var myBulletsArray = new Array();//我的子弹组 数组
var explosionsArray = new Array();//爆炸效果数组
var SkillToolArray = new Array();//道具数组
var DownDiamendArray = new Array();
//宠物
var imgs_anim_small;
var imgs_anim_bullet;
var img_levelup;
var img_warning;
var levelupObject;
var warningObject;
var myPlane;
var img_sheild;
var imgs_thunder;
var imgs_myPlane;
var imgs_enermyFly1;
var imgs_boss;
var imgs_bullet;
var imgs_myBullet;
var imgs_explosion;
var imgs_tool;
var imgs_diamend;

var img_bar_frame;
var img_bar_experience;
var img_bar_blood;

var movement = 0;
var currentMyLevel = 0;
var currentMyHP = 0;
var levelTotalHP = 0;
var currentMyEX = 0;
//var levelTotalEX = 0;
var levelBaseBulletAttack = 0;
var AttackTypeMax = 9;

var totalGotCoin = 0;//获取的金币数目
var rewardToolArray = new Array();//闯关奖励的数组

var tapX = -1;
var tapY = -1;
var lastTouchX = -1;
var lastTouchY = -1;
var touchX = -1;
var touchY = -1;
var isMoveing = false;

var isBossComing = false;
var bossLevelArray;

var isScreenRed = false;
var screenRedCount = 0;

var isMyPlaneInvincible = false;
var MyPlaneInvincibleCount = 0;

var isDead = false;
var deadCount = 0;
var deadScreenShot;

var isKickBomb = false;
var KickBombCount = 0;

var isGameHasArrivalLimit;//游戏是否有波数限制
var isStartFromBlackStage = false;//是否从暗黑世界启动游戏
var targetArrivalIndex;//目标波数
var needGotoWinScreen = false;//暗黑世界BOSS打死效果释放后胜利
var winScreenCount = 0;
var isStartCheckWin = false;
var hasWinGameMission = false;

var restartTimes = 0;//复活的次数
var restartCostDiamond = 20;//复活所需的钻石
//结束界面
var black_alpha;
var img_font_gameover;
//分数结算页面
var img_got_bg;
var img_got_btngo;
var img_num_xxbig;
var imgs_got_tool;
var hasUseDoubleMoney = false;
//暂停界面
var img_font_gamepause;
//游戏指导界面
var isNewPlayer = true;//是否为新手
var needGuide = false;//是否新手指导
var guideCount = 0;
var guideIndex = 0;//引导序号
var guideCountArray = [5,100,230,440,560];
var imgs_guide;

var hasLottoChance;

var iphoneActivySound = false;
//触屏事件回调函数
function dealTouchEvent(event){
    if(!iphoneActivySound && loadGameOK){
        iphoneActivySound = true;
        setSoundtrack();
    }
    if(event.type == "dragstart"){
        touchX = event.gesture.touches[0].pageX;
        touchY = event.gesture.touches[0].pageY;
        lastTouchX = touchX;
        lastTouchY = touchY;
    }
    else if(event.type == "drag"){
        touchX = event.gesture.touches[0].pageX;
        touchY = event.gesture.touches[0].pageY;
        isMoveing = true;
    }
    else if(event.type == "dragend"){
        touchX = -1;
        touchY = -1
        lastTouchX = touchX;
        lastTouchY = touchY;
        isMoveing = false;
    }
    else if(event.type == "tap"){
        tapX = event.gesture.touches[0].pageX;
        tapY = event.gesture.touches[0].pageY;
    }
}

function loadProgress(){
    loadCount++;
}

function createImage(name, whenloaded){
    loadTotal++;
    var img = new Image();
    img.src = "airwarimg/"+name+".png";
    img.onload = whenloaded;
    img.onerror = whenloaded;
    return img;
}
function main(){
    initYIDONGParamater();
//    userRegister();
//   setAudio();
    sizeHandler();
    $(window).bind('orientationchange resize', function(e){
        sizeHandler()
    });

    jsGameScreen = document.getElementById("jsGameScreen");
    SW  = jsGameScreen.width;
    SH = jsGameScreen.height;

    //加载前所需的初始化
//    load_drawStartX = SW>>1;
//    load_drawStartY = SH>>1;
//    load_colorArray = new Array();
//    for(var i = 0; i< 24;i++){
//        load_colorArray.push("rgb(255,255,255)");
//    }
//    load_basecolorSum = new Array();
//    load_basecolorSum.push("rgb(255, 0, 0)");
//    load_basecolorSum.push("rgb(255, 165, 0)");
//    load_basecolorSum.push("rgb(255, 255, 0)");
//    load_basecolorSum.push("rgb(0, 255, 0)");
//    load_basecolorSum.push("rgb(0, 127, 255)");
//    load_basecolorSum.push("rgb(0, 0, 255)");
//    load_basecolorSum.push("rgb(139, 0, 255)");

    circleRoundXYArray = new Array();
//    setCircleRoundXYArray(load_banjing);
    canvas = jsGameScreen.getContext("2d");
    canvas.font = "14px Arial";
    loadState = -1;
    img_startpage_icon = createImage("startpage_icon", loadProgress);
//    img_startpage_font1 = createImage("startpage_font1", loadProgress);
    img_startpage_font2 = createImage("startpage_font2", loadProgress);
    img_startpage_star = createImage("startpage_star", loadProgress);
    img_startpage_lower = createImage("startpage_lower", loadProgress);
    img_startpage_upper = createImage("startpage_upper", loadProgress);

    toast4JS = new Toast4JS();
    lottoScreen = new LottoScreen();
    alertDialog = new AlertDialog();
    loading4JS = new Loading4JS();
    chargeDialog = new ChargeDialog();

    function logic(timestamp){
        //计算2次逻辑的时间间隔
        var logicStart = Date.now();
        var diff = logicStart - startTime;
        //使用diff确定下一步的绘制时间
        if(diff > CycleTime){
            //吧startTime重写为这一次的时间
            startTime = logicStart;

            update();

//            canvas.clearRect(0,0, SW, SH);
            draw();
            lottoScreen.paint();
            alertDialog.paint();
            chargeDialog.paint();
            loading4JS.paint();
            toast4JS.paint();
        }
        //执行下一次逻辑
        window.requestAFrame(logic);
    }
    startTime = Date.now();
    requestId  = window.requestAFrame(logic);
}

function loadResource(){
    img_dialoBg = createImage("alertdialog_bg", loadProgress);
    img_loading = createImage("loading", loadProgress);

    img_charge_back = createImage("charge_back", loadProgress);
    img_charge_barbg = createImage("charge_barbg", loadProgress);
    img_charge_btn_coin = createImage("charge_btn_coin", loadProgress);
    img_charge_btn_diamond = createImage("charge_btn_diamond", loadProgress);
    img_charge_font_b = createImage("charge_font_b", loadProgress);
    img_charge_ic_coin = createImage("charge_ic_coin", loadProgress);
    img_charge_ic_diamond = createImage("charge_ic_diamond", loadProgress);
    img_charge_next = createImage("charge_next", loadProgress);
    img_charge_title = createImage("charge_title", loadProgress);
    img_charge_upper = createImage("charge_upper", loadProgress);
    img_charge_x = createImage("charge_x", loadProgress);

    img_lotto_bg = createImage("lotto_bg", loadProgress);
    img_lotto_bg_light = createImage("lotto_bg_light", loadProgress);
    img_lotto_bg_round = createImage("lotto_bg_round", loadProgress);
    img_lotto_btn_cost = createImage("lotto_btn_cost", loadProgress);
    img_lotto_btn_free = createImage("lotto_btn_free", loadProgress);
    imgs_lotto_light = new Array();
    for(var i = 0; i< 12; i++){
        imgs_lotto_light.push(createImage("lotto_light"+i, loadProgress));
    }
    img_lotto_npc = createImage("lotto_npc", loadProgress);



    img_btntools = new Array();
    for(var i = 0; i< 3; i++){
        img_btntools.push(createImage("btn_tool"+i, loadProgress));
    }
    img_btnPause = createImage("btn_pause", loadProgress);
    img_btnNoSound = createImage("btn_nosound", loadProgress);
    img_btnSound = createImage("btn_sound", loadProgress);
    black_bg = createImage("black_bg", loadProgress);
    red_bg = createImage("red_bg", loadProgress);
    logo_bg = createImage("menubg", loadProgress);
    logo_name = createImage("logo", loadProgress);
    logo_opera = createImage("pressx", loadProgress);
    black_cloud = createImage("cloudsblack", loadProgress);
    img_font_gamewin = createImage("font_gamewin", loadProgress);

    game_bg_array = new Array();
    for(var i = 0; i< 4; i++){
        game_bg_array.push(createImage("img_bg_level_"+i, loadProgress));
    }
    white_cloud = createImage("clouds", loadProgress);
    scoreImg = createImage("score", loadProgress);
    font_hp = createImage("font_hp", loadProgress);
    font_lv = createImage("font_lv", loadProgress);

    imgs_enermyFly1 = new Array();
    for(var i = 0; i< 6; i++){
        for(var j = 0; j< 2; j++){
            imgs_enermyFly1.push(createImage("enemy_"+i+"_"+j, loadProgress));
        }
    }

    imgs_boss = new Array();
    imgs_boss.push(createImage("boss_1_default", loadProgress));
    imgs_boss.push(createImage("boss_1_hit", loadProgress));
    imgs_boss.push(createImage("boss_2_default", loadProgress));
    imgs_boss.push(createImage("boss_2_hit", loadProgress));
    imgs_boss.push(createImage("boss_3_default", loadProgress));
    imgs_boss.push(createImage("boss_3_hit", loadProgress));
    imgs_boss.push(createImage("boss_4_default", loadProgress));
    imgs_boss.push(createImage("boss_4_hit", loadProgress));

    imgs_bullet = new Array();
    imgs_bullet.push(createImage("bullet_0", loadProgress));
    imgs_bullet.push(createImage("bullet_1", loadProgress));
    imgs_bullet.push(createImage("bullet_2", loadProgress));
    imgs_bullet.push(createImage("bullet_3", loadProgress));

    imgs_myBullet = new Array();
    for(var i = 0; i< 7; i++){
        imgs_myBullet.push(createImage("player_bullet_"+i, loadProgress));
    }

    imgs_explosion = new Array();
    for(var j = 0; j< 2; j++){
        var explosion1 = new Array();
        for(var i = 0; i< 7; i++){
            explosion1.push(createImage("explosion"+j+"_"+i, loadProgress));
        }
        imgs_explosion.push(explosion1);
    }
    imgs_explosion.push(createImage("ex_bullet", loadProgress));

    imgs_tool = new Array();
    for(var j = 0; j< 3; j++){
        var tool0 = new Array();
        for(var i = 0; i< 2; i++){
            tool0.push(createImage("powerup"+j+"_"+i, loadProgress));
        }
        imgs_tool.push(tool0);
    }

    imgs_diamend = new Array();
    for(var j = 0; j< 2; j++){
        var tool0 = new Array();
        for(var i = 0; i< 4; i++){
            tool0.push(createImage("diamend"+j+"_"+i, loadProgress));
        }
        imgs_diamend.push(tool0);
    }

    imgs_myPlane = new Array();
    for(var i = 0; i< 4; i++){
        var tempMyPlanes = new Array();
        for(var j = 0; j< 6; j++){
            tempMyPlanes.push(createImage("p"+i+"_"+j, loadProgress));
        }
        imgs_myPlane.push(tempMyPlanes);
    }

    img_levelup = createImage("font_levelup", loadProgress);
    img_warning = createImage("font_warning", loadProgress);

    img_sheild = createImage("shield", loadProgress);
    imgs_thunder = new Array();
    for(var i = 0; i< 3; i++){
        imgs_thunder.push(createImage("thunder_"+i, loadProgress));
    }

    black_alpha = createImage("black_alpha", loadProgress);
    img_font_gameover = createImage("font_gameover", loadProgress);
    img_font_gamepause = createImage("font_gamepause", loadProgress);

    img_got_bg = createImage("got_bg", loadProgress);
    img_got_btngo = createImage("got_btngo", loadProgress);
    img_num_xxbig = createImage("num_xxbig", loadProgress);
    imgs_got_tool = new Array();
    for(var i = 0; i< 6; i++){
        imgs_got_tool.push(createImage("got_tool"+i, loadProgress));
    }

    img_bar_frame = createImage("bar_frame", loadProgress);
    img_bar_experience = createImage("bar_experience", loadProgress);
    img_bar_blood = createImage("bar_blood", loadProgress);

    select_bg = createImage("select_bg", loadProgress);
    imgs_driver_b = new Array();
    for(var i = 0; i< 4; i++){
        imgs_driver_b.push(createImage("driver_b"+i, loadProgress));
    }
//    imgs_driver_n = new Array();
//    for(var i = 0; i< 4; i++){
//        imgs_driver_n.push(createImage("driver_n"+i, loadProgress));
//    }
    img_select_choose = createImage("select_choose", loadProgress);
    img_select_buy_d = createImage("select_buy_d", loadProgress);
    img_select_kick = createImage("select_kick", loadProgress);
    img_num_small = createImage("num_small", loadProgress);
    img_planename = createImage("planename", loadProgress);
    img_planestar = createImage("planestar", loadProgress);
    img_select_dice = createImage("select_dice", loadProgress);
    img_select_rank = createImage("select_rank", loadProgress);
    img_select_anim = createImage("select_anim", loadProgress);
    img_select_store = createImage("select_store", loadProgress);
    img_select_daily = createImage("select_daily", loadProgress);

    img_blackhole_bg = createImage("blackhole_bg", loadProgress);
    img_btn_start_yes = createImage("btn_start_yes", loadProgress);

    img_btn_back = createImage("btn_back", loadProgress);
    img_p_levelup_bg = createImage("p_levelup_bg", loadProgress);
    img_btn_strang_yes = createImage("btn_strang_yes", loadProgress);
    img_btn_strang_no = createImage("btn_strang_no", loadProgress);
    img_p_levelup_move = createImage("p_levelup_move", loadProgress);
    img_num_levelup = createImage("num_levelup", loadProgress);

    img_anim_center_bg = createImage("anim_center_bg", loadProgress);
//    imgs_driver_sn = new Array();
//    for(var i = 0; i< 4; i++){
//        imgs_driver_sn.push(createImage("driver_sn"+i, loadProgress));
//    }
    img_animname_bg = createImage("animname_bg", loadProgress);
    imgs_anim_b = new Array();
    for(var i = 0; i< 6; i++){
        imgs_anim_b.push(createImage("anim_b"+i, loadProgress));
    }
    img_font_cur_material  = createImage("font_cur_material", loadProgress);
    imgs_anim_egg = new Array();
    for(var i = 0; i< 2; i++){
        imgs_anim_egg.push(createImage("anim_egg"+i, loadProgress));
    }
    img_font_unlock  = createImage("font_unlock", loadProgress);
    img_anim_lock  = createImage("anim_lock", loadProgress);
    img_btn_lock_yes  = createImage("btn_lock_yes", loadProgress);
    img_btn_lock_no  = createImage("btn_lock_no", loadProgress);
    img_effect_light = createImage("effect_light", loadProgress);

    imgs_anim_small = new Array();
    for(var i = 0; i< 6; i++){
        imgs_anim_small.push(createImage("anim_s"+i, loadProgress));
    }
    imgs_anim_bullet = new Array();
    for(var i = 0; i< 6; i++){
        imgs_anim_bullet.push(createImage("anim_blt"+i, loadProgress));
    }

    img_coin_pre = createImage("coin_pre", loadProgress);
    img_coin_add = createImage("coin_add", loadProgress);
    img_diamond_pre = createImage("diamond_pre", loadProgress);
    img_diamond_add = createImage("diamond_add", loadProgress);
    img_num_money = createImage("num_money", loadProgress);

    imgs_driver_s = new Array();
    for(var i = 0; i< 4; i++){
        imgs_driver_s.push(createImage("driver_s"+i, loadProgress));
    }

    tool_bg = createImage("tool_bg", loadProgress);

    if(isNewPlayer){
        imgs_guide = new Array();
        for(var i = 1; i<= 5; i++){
            imgs_guide.push(createImage("font_guide_"+i, loadProgress));
        }
    }

    img_mission_bg = createImage("mission_bg", loadProgress);
    img_mission_select = createImage("mission_select", loadProgress);
    img_mission_star = createImage("mission_star", loadProgress);
    img_mission_star_u = createImage("mission_star_u", loadProgress);

    loadTotal = loadTotal + preCache();
}

function firstInitGame(){
    //初始化toast
    toast4JS.firstInit(SW, SH, 25, canvas);
    //初始化alertDialog
    alertDialog.firstInit(SW, SH, img_dialoBg, 18,16, canvas);
    //初始化Loading4JS
    loading4JS.firstInit(SW, SH, 16, canvas, black_alpha, img_loading);
    //初始化chargedialog
    chargeDialog.firstInit(SW, SH, black_alpha, img_charge_title, img_charge_back, img_charge_barbg, img_charge_x, img_charge_font_b, img_num_small, img_charge_ic_diamond, img_charge_ic_coin, img_charge_btn_diamond, img_charge_btn_coin, img_charge_upper, img_charge_next, canvas);
    //初始化lottoScreen
    lottoScreen.firstInit(black_alpha, img_charge_back, img_lotto_bg , img_lotto_npc, img_lotto_bg_light, img_lotto_bg_round, img_lotto_btn_cost, img_lotto_btn_free, imgs_lotto_light, canvas);

    //事件绑定
    var HammerConfig = {
        preventDefault: true
    };
    var hammertime0 = Hammer(jsGameScreen,HammerConfig).on("tap", dealTouchEvent);
    var hammertime1 = Hammer(jsGameScreen,HammerConfig).on("dragstart", dealTouchEvent);
    var hammertime2 = Hammer(jsGameScreen,HammerConfig).on("dragend", dealTouchEvent);
    var hammertime3 = Hammer(jsGameScreen,HammerConfig).on("drag", dealTouchEvent);

    //4个boss
    bossLevelArray = new Array();
    bossLevelArray.push(0);
    bossLevelArray.push(0);
    bossLevelArray.push(0);
    bossLevelArray.push(0);

    levelupObject = new Rise_LEVELUP();
    warningObject = new Rise_StayFirst();

    myPlane = new MyPlane();
}

function initGame(){
    if(isGameHasArrivalLimit){
        if(isStartFromBlackStage){
            //如果从暗黑世界启动
            arrivalIndex = currentBlackStage*20-1;//当前第几波
            targetArrivalIndex = arrivalIndex+1;
        }
        else{
            currentMapIndex = randomNextInt(0,3);
            arrivalIndex = zhengchu(startMissionIndex,3)*20+6*(startMissionIndex%3);//当前第几波
            if(startMissionIndex%3 == 2){
                targetArrivalIndex = (zhengchu(startMissionIndex,3)+1)*20;
            }
            else{
                targetArrivalIndex = arrivalIndex+6;
            }
        }

        winScreenCount = 0;
        isStartCheckWin = false;
        hasWinGameMission = false;
//        var level = zhengchu(arrivalIndex-1, 20);
        currentMyLevel = myPlaneLevel[fightDriverIndex]-1;
        needGuide = false;
    }
    else if(isNewPlayer){
        needGuide = true;
        guideCount = 0;
        guideIndex = 0;
        currentMapIndex = 3;
        arrivalIndex = 61;//当前第几波
        currentMyLevel = 2;
    }
    else{
        currentMapIndex = 0;
        arrivalIndex = 0;//当前第几波
        currentMyLevel = myPlaneLevel[fightDriverIndex]-1;
    }
    changingMap = false;
    mapMoveCount = mapMoveTotal = 0;
    nextArrivalDis = 100;
    defeatEnermyCount = 0;
    planesArray.splice(0,planesArray.length);
    bulletArray.splice(0,bulletArray.length);
    myBulletsArray.splice(0,myBulletsArray.length);
    explosionsArray.splice(0,explosionsArray.length);
    SkillToolArray.splice(0,SkillToolArray.length);
    DownDiamendArray.splice(0,DownDiamendArray.length);
    levelupObject.visible = false;
    warningObject.visible = false;

    movement = 4;
    currentMyHP = 0;
    levelTotalHP = 0;
    currentMyEX = 0;
//    levelTotalEX = 0;
    levelBaseBulletAttack = 0;

    var tempAnimSmallImg = new Array();
    for(var i = 0; i < animLevelArray.length; i++){
        if(animLevelArray[i] > 0){
            tempAnimSmallImg.push(imgs_anim_small[i]);
        }
    }

    myPlane.InitPlane(null, img_sheild, imgs_thunder, SW-124>>1, 600, 124, 124, SW, SH, 0, 500, tempAnimSmallImg);
    myPlaneLevelUp();

    isBossComing = false;
    bossLevelArray[0] = 0;
    bossLevelArray[1] = 0;
    bossLevelArray[2] = 0;
    bossLevelArray[3] = 0;

    isScreenRed = false;
    isMyPlaneInvincible = false;
    isKickBomb = false;
    totalGotCoin = 0;
    rewardToolArray.splice(0,rewardToolArray.length);

    restartTimes = 0;
    restartCostDiamond = 20;
}

function effectCounter(){
    effect_count++;
    if(effect_count>= Number.MY_MAX_VALUE){
        effect_count = 0;
    }
}

function changeState(targetState){
    state = targetState;
    effect_count = 0;
    //清空按键
    tapX = -1;
    tapY = -1;
    touchX = -1;
    touchY = -1;
    lastTouchX = -1;
    lastTouchY = -1;
    isMoveing = false;
}

function update(){
    if(!loading4JS.logic()){
        input();
        if(!chargeDialog.isShow()){
            logic();
            lottoScreen.logic();
        }
    }
    else{
        tapX = -1;
        tapY = -1;
    }
    toast4JS.logic();
}

function input(){
    if(tapX > 0 && tapY > 0){
        var tempTapX = (tapX-Dis_left)/gamescaleX;
        var tempTapY = (tapY-Dis_top)/gamescaleY;

        if(chargeDialog.input(tempTapX, tempTapY)){
            tapX = -1;
            tapY = -1;
            return;
        }
        if(alertDialog.input(tempTapX, tempTapY)){
            tapX = -1;
            tapY = -1;
            return;
        }
        if(lottoScreen.input(tempTapX, tempTapY)){
            tapX = -1;
            tapY = -1;
            return;
        }

        switch (state){
            case 1:
                getUserInfo();
                break;

            case 6:
                if(isTouch_inArea(tempTapX,tempTapY, 311, 644, 311+148, 644+104)){
                    //道具购买界面
                    isGameHasArrivalLimit = false;
                    isStartFromBlackStage = false;
                    //记录名字
                    if(!hasChooseNameArray[fightDriverIndex]){
                        hasChooseNameArray[fightDriverIndex] = true;

                        loading4JS.showLoading("正在提交请求，请等待！");
                        UpdatePlayer(function(){
                            loading4JS.closeLoading();
                            backState = 6;
                            changeState(7);
                        });
                    }
                    else{
                        backState = 6;
                        changeState(7);
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 170, 540, 170+179, 540+52)){
                    if(myPlaneLevel[driverIndex] > 0){
                        if(driverIndex != fightDriverIndex){
                            //选择出战
                            fightDriverIndex = driverIndex;

                            loading4JS.showLoading("正在提交请求，请等待！");
                            updateFightPlayer(function(){
                                loading4JS.closeLoading();
                            });
                        }
                    }
                    else{
                        //购买
                        if(myDiamond >= BuyDriverCostArray[driverIndex]){
                            myDiamond = myDiamond - BuyDriverCostArray[driverIndex];
                            myPlaneLevel[driverIndex] = 1;
                            myFightAttack[driverIndex] = 5000 + (myPlaneLevel[driverIndex]-1)*500;

                            loading4JS.showLoading("正在提交请求，请等待！");
                            UpdatePlaneLevel(function(){
                                loading4JS.closeLoading();
                                toast4JS.buildToast("成功购买战机！", 1500);
                            });
                        }
                        else{
                            toast4JS.buildToast("没有足够的钻石！", 1500);
                        }
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, CoinBtnStartX, 12, CoinBtnStartX+33, 12+33)){
                    //充值商店界面-金币
                    chargeDialog.show(false);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, DiamondBtnStartX, 12, DiamondBtnStartX+33, 12+33)){
                    //充值商店界面-钻石
                    chargeDialog.show(true);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 420, 142, 420+85, 142+41)){
                    //排行榜界面
                    window.open(backDomain+"/airwarback/getrank?pageNum=1");
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 203, 628, 203+102, 628+123)){
                    //每日领奖
                    lottoScreen.show();
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 422, 60, 422+81, 60+73)){
                    //充值商店界面
                    chargeDialog.show(true);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 0, 165, 145, 273)){
                    driverIndex--;
                    if(driverIndex<0){
                        driverIndex = 3;
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 350, 165, 512, 273)){
                    driverIndex++;
                    if(driverIndex>3){
                        driverIndex = 0;
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 344, 300, 344+35, 300+39)){
                    //随机驾驶员名字
                    if(myPlaneLevel[driverIndex] > 0){
                        if(!hasChooseNameArray[driverIndex]){
                            var tempIndex = randomNextInt(0,19);
                            driverNameIndex[driverIndex] = tempIndex;
                            driverName[driverIndex] = DriverNameArray[driverIndex][tempIndex];
                        }
                    }
                    else{
                        toast4JS.buildToast("请先购买战机！", 1500);
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 190, 350, 190+124, 350+124)){
                    //战机升级界面
                    if(myPlaneLevel[driverIndex] > 0){
                        checkPlaneLevelUpNeed(myPlaneLevel[driverIndex]);
                        changeState(9);
                    }
                    else{
                        toast4JS.buildToast("请先购买战机！", 1500);
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 10, 60, 10+64, 60+71)){
                    //宠物界面
                    animSelectIndex = 0;
                    checkPlaneLevelUpNeed(animLevelArray[animSelectIndex]);
                    changeState(10);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 42, 644, 42+150, 644+111)){
                    //暗黑世界 界面
                    //记录名字
                    if(!hasChooseNameArray[fightDriverIndex]){
                        hasChooseNameArray[fightDriverIndex] = true;

                        loading4JS.showLoading("正在提交请求，请等待！");
                        UpdatePlayer(function(){
                            loading4JS.closeLoading();
                            updateMissionSelectIndex();
                            changeState(12);
                        });
                    }
                    else{
                        updateMissionSelectIndex();
                        changeState(12);
                    }
                }
                break;

            case 7:
                if(isTouch_inArea(tempTapX,tempTapY, 160, 560, 350, 630)){
                    //开始游戏
                    changeState(2);
                    initGame();
                }
                else if(isTouch_inArea(tempTapX,tempTapY, CoinBtnStartX, 12, CoinBtnStartX+33, 12+33)){
                    //充值商店界面-金币
                    chargeDialog.show(false);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, DiamondBtnStartX, 12, DiamondBtnStartX+33, 12+33)){
                    //充值商店界面-钻石
                    chargeDialog.show(true);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 348, 214, 455, 266)){
                    //买双倍金币
                    if(myCoin >= ToolCostArray[0]){
                        myCoin = myCoin - ToolCostArray[0];
                        myToolArray[0] = myToolArray[0] + 1;

                        loading4JS.showLoading("正在提交请求，请等待！");
                        UpdateTool(function(){
                            loading4JS.closeLoading();
                            toast4JS.buildToast("成功购买双倍积分！", 1500);
                        });
                    }else{
                        toast4JS.buildToast("没有足够的金币！", 1500);
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 348, 294, 455, 346)){
                    //买进阶
                    if(myDiamond >= ToolCostArray[1]){
                        myDiamond = myDiamond - ToolCostArray[1];
                        myToolArray[1] = myToolArray[1] + 1;

                        loading4JS.showLoading("正在提交请求，请等待！");
                        UpdateTool(function(){
                            loading4JS.closeLoading();
                            toast4JS.buildToast("成功购买核爆！", 1500);
                        });
                    }else{
                        toast4JS.buildToast("没有足够的钻石！", 1500);
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 348, 376, 455, 426)){
                    //买冲刺
                    if(myDiamond >= ToolCostArray[2]){
                        myDiamond = myDiamond - ToolCostArray[2];
                        myToolArray[2] = myToolArray[2] + 1;

                        loading4JS.showLoading("正在提交请求，请等待！");
                        UpdateTool(function(){
                            loading4JS.closeLoading();
                            toast4JS.buildToast("成功购买冲刺！", 1500);
                        });
                    }else{
                        toast4JS.buildToast("没有足够的钻石！", 1500);
                    }
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 348, 456, 455, 509)){
                    //买核爆
                    if(myDiamond >= ToolCostArray[3]){
                        myDiamond = myDiamond - ToolCostArray[3];
                        myToolArray[3] = myToolArray[3] + 1;

                        loading4JS.showLoading("正在提交请求，请等待！");
                        UpdateTool(function(){
                            loading4JS.closeLoading();
                            toast4JS.buildToast("成功购买暴走！", 1500);
                        });
                    }else{
                        toast4JS.buildToast("没有足够的钻石！", 1500);
                    }
                }
//                tapX = -1;
//                tapY = -1;
                break;

            case 2:
//                if(tapX > 0 && tapY > 0){
//                console.log("Dis_left:"+Dis_left+"  Dis_top:"+Dis_top+"   tapX:"+tapX+"tapY:"+tapY+"   tapX-Dis_left:"+(tapX-Dis_left)+" tapY-Dis_top:"+(tapY-Dis_top)+"  tempTapX:"+tempTapX+"tempTapY:"+tempTapY);
                    if(!isDead){
                        if(isTouch_inArea(tempTapX,tempTapY,SW-54-10,35,SW-54-10+54,35+54)){
                            //暂停
                            deadScreenShot = canvas.getImageData(0,0,SW,SH);
                            changeState(4);
                        }
                        else if(isTouch_inArea(tempTapX,tempTapY,SW-54-10,35+54+25,SW-54-10+54,35+54+25+54)){
                            //声音
                            if(SoundEnable){
                                //禁音
                                SoundEnable = false;
                            }
                            else{
                                //启用
                                SoundEnable = true;
                            }
                            musicMode();
                        }
                        else if(isTouch_inArea(tempTapX,tempTapY,20,550,20+60,550+60)){
                            //炸弹
                            if(needGuide){
                                if(!isKickBomb){
                                    creatBomb();
                                }
                            }else{
                                if(myToolArray[1] > 0){
                                    if(!isKickBomb){
                                        myToolArray[1] = myToolArray[1] - 1;
                                        creatBomb();
                                    }
                                }else{
                                    deadScreenShot = canvas.getImageData(0,0,SW,SH);
                                    changeState(13);

                                    alertDialog.setIconImg(img_btntools[0]);
                                    alertDialog.setTitle("道具不足，需要购买");
                                    alertDialog.setMessage("花费10钻石购买吗？");
                                    alertDialog.setOkButtonListener(function(){
                                        if(myDiamond >= ToolCostArray[1]){
                                            myDiamond = myDiamond - ToolCostArray[1];
                                            myToolArray[1] = myToolArray[1] + 1;

                                            loading4JS.showLoading("正在提交请求，请等待！");
                                            UpdateTool(function(){
                                                loading4JS.closeLoading();
                                                toast4JS.buildToast("成功购买核爆!", 1000)
                                                //返回游戏
                                                alertDialog.clear();
                                                changeState(2);
                                                deadScreenShot = null;
                                            });
                                        }else{
                                            if(confirm("没有足够的钻石!前往充值？")){
                                                chargeDialog.show(true);
                                            }
                                        }
                                    });
                                    alertDialog.setCancelButtonListener(function(){
                                        changeState(2);
                                        deadScreenShot = null;
                                    });
                                    alertDialog.show();
                                }
                            }
                        }
                        else if(isTouch_inArea(tempTapX,tempTapY,20,550-76-20,20+60,550-76-20+60)){
                            //冲刺
                            if(needGuide){
                                if(!myPlane.hasThunder && !isBossComing){
                                    creatThunder();
                                }
                            }else{
                                if(myToolArray[2] > 0){
                                    if(!myPlane.hasThunder && !isBossComing){
                                        myToolArray[2] = myToolArray[2] - 1;
                                        creatThunder();
                                    }
                                }else{
                                    deadScreenShot = canvas.getImageData(0,0,SW,SH);
                                    changeState(13);

                                    alertDialog.setIconImg(img_btntools[1]);
                                    alertDialog.setTitle("道具不足，需要购买");
                                    alertDialog.setMessage("花费10钻石购买吗？");
                                    alertDialog.setOkButtonListener(function(){
                                        if(myDiamond >= ToolCostArray[2]){
                                            myDiamond = myDiamond - ToolCostArray[2];
                                            myToolArray[2] = myToolArray[2] + 1;

                                            loading4JS.showLoading("正在提交请求，请等待！");
                                            UpdateTool(function(){
                                                loading4JS.closeLoading();
                                                toast4JS.buildToast("成功购买冲刺!", 1000);
                                                //返回游戏
                                                alertDialog.clear();
                                                changeState(2);
                                                deadScreenShot = null;
                                            });
                                        }else{
                                            if(confirm("没有足够的钻石!前往充值？")){
                                                chargeDialog.show(true);
                                            }
                                        }
                                    });
                                    alertDialog.setCancelButtonListener(function(){
                                        changeState(2);
                                        deadScreenShot = null;
                                    });
                                    alertDialog.show();
                                }
                            }
                        }
                        else if(isTouch_inArea(tempTapX,tempTapY,20,550-152-40,20+60,550-152-40+60)){
                            if(needGuide){
                                if(myPlane.attackType < AttackTypeMax){
                                    myPlane.attackType = AttackTypeMax;
                                }
                            }else{
                                if(myToolArray[3] > 0){
                                    if(myPlane.attackType < AttackTypeMax){
                                        myToolArray[3] = myToolArray[3] - 1;
                                        myPlane.attackType = AttackTypeMax;
                                    }
                                }else{
                                    deadScreenShot = canvas.getImageData(0,0,SW,SH);
                                    changeState(13);

                                    alertDialog.setIconImg(img_btntools[2]);
                                    alertDialog.setTitle("道具不足，需要购买");
                                    alertDialog.setMessage("花费10钻石购买吗？");
                                    alertDialog.setOkButtonListener(function(){
                                        if(myDiamond >= ToolCostArray[3]){
                                            myDiamond = myDiamond - ToolCostArray[3];
                                            myToolArray[3] = myToolArray[3] + 1;

                                            loading4JS.showLoading("正在提交请求，请等待！");
                                            UpdateTool(function(){
                                                loading4JS.closeLoading();
                                                toast4JS.buildToast("成功购买进阶!", 1000)
                                                //返回游戏
                                                alertDialog.clear();
                                                changeState(2);
                                                deadScreenShot = null;
                                            });
                                        }else{
                                            if(confirm("没有足够的钻石!前往充值？")){
                                                chargeDialog.show(true);
                                            }
                                        }
                                    });
                                    alertDialog.setCancelButtonListener(function(){
                                        changeState(2);
                                        deadScreenShot = null;
                                    });
                                    alertDialog.show();
                                }
                            }
                        }
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 3:
                //game over
//                if(tapX > 0 && tapY > 0){
                    if(isTouch_inArea(tempTapX,tempTapY,51+60,290+104,51+60+120,290+104+75)){
                        //复活
                        if(myDiamond >= restartCostDiamond){
//                            planeRevive();
                            restartTimes++;
                            myDiamond = myDiamond - restartCostDiamond;


                            myPlane.visiable = true;
                            currentMyHP = levelTotalHP;
                            //改变位置
                            myPlane.PlaneX = SW-124>>1;
                            myPlane.PlaneY = 600;
                            //添加护盾
                            myPlane.creatShield();
                            //子弹升级
                            myPlane.attackType = 3;
                            changeState(2);
                            //清理
                            deadScreenShot = null;
                        }
                        else{
                            if(confirm("没有足够的钻石!前往充值？")){
                                chargeDialog.show(true);
                            }
                        }
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY,51+216,290+104,51+216+120,290+104+75)){
                        if(isGameHasArrivalLimit){
                            //返回
                            if(backState == 6){
                            }
                            else if(backState == 11){
                                //boss
                                updateBossUnlock();
                            }
                            else if(backState == 12){
                                //关卡
                                updateMissionSelectIndex();
                            }
                            changeState(backState);
                            //清理
                            deadScreenShot = null;
                        }
                        else{
                            var level = zhengchu(arrivalIndex-1, 20);
                            var numSum = zhengchu(level*(level+1), 2);
                            totalGotCoin = 50*numSum + zhengchu(arrivalIndex*(arrivalIndex - 1), 2);

                            if(myToolArray[0] > 0){
                                myToolArray[0] = myToolArray[0] - 1;
                                hasUseDoubleMoney = true;
                                totalGotCoin = totalGotCoin*2;
                            }
                            else{
                                hasUseDoubleMoney = false;
                            }

                            var tempGotArray;

                            tempGotArray = new Array();
                            tempGotArray.push(0);
                            tempGotArray.push(numSum);
                            rewardToolArray.push(tempGotArray);
                            //残骸
                            tempGotArray = new Array();
                            tempGotArray.push(1);
                            tempGotArray.push(numSum);
                            rewardToolArray.push(tempGotArray);
                            //随机宠物材料
                            var totalTypeArray = [2,3,4,5];
                            var tempTypeIndex = randomNextInt(0,3);
                            tempGotArray = new Array();

                            tempGotArray.push(totalTypeArray[tempTypeIndex]);
                            tempGotArray.push(numSum);
                            rewardToolArray.push(tempGotArray);
                            //随机宠物材料
                            totalTypeArray.splice(tempTypeIndex,1);
                            tempTypeIndex = randomNextInt(0,2);
                            tempGotArray = new Array();

                            tempGotArray.push(totalTypeArray[tempTypeIndex]);
                            tempGotArray.push(numSum);
                            rewardToolArray.push(tempGotArray);
                            //更新我的金币和材料数组
                            myCoin = myCoin + totalGotCoin;
                            myMaterialArray[0] = myMaterialArray[0] + numSum;
                            myMaterialArray[1] = myMaterialArray[1] + numSum;
                            myMaterialArray[rewardToolArray[2][0]] = myMaterialArray[rewardToolArray[2][0]] + numSum;
                            myMaterialArray[rewardToolArray[3][0]] = myMaterialArray[rewardToolArray[3][0]] + numSum;
                            //更新历史最大分数
                            if(currentMyEX > myMaxScore){
                                myMaxScore = currentMyEX;
                            }

                            loading4JS.showLoading("正在提交请求，请等待！");
                            UpdateTotal(function(){
                                loading4JS.closeLoading();
                                changeState(8);
                            });
                        }
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 8:
//                if(tapX > 0 && tapY > 0){
                    if(isTouch_inArea(tempTapX,tempTapY, 192, 648, 192+127, 648+50)){
                        //返回
                        if(backState == 6){
                        }
                        else if(backState == 11){
                            //boss
                            updateBossUnlock();
                        }
                        else if(backState == 12){
                            //关卡
                            updateMissionSelectIndex();
                        }
                        changeState(backState);
                        //清理
                        deadScreenShot = null;
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 4:
                //暂停
//                if(tapX > 0 && tapY > 0){
                    if(isTouch_inArea(tempTapX,tempTapY,51+60,290+104,51+60+120,290+104+75)){
                        changeState(2);
                        //清理
                        deadScreenShot = null;
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY,51+216,290+104,51+216+120,290+104+75)){
                        //返回
                        isNewPlayer = false;
                        needGuide = false;

                        if(backState == 6){
                        }
                        else if(backState == 11){
                            //boss
                            updateBossUnlock();
                        }
                        else if(backState == 12){
                            //关卡
                            updateMissionSelectIndex();
                        }
                        changeState(backState);

                        //清理
                        deadScreenShot = null;
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 5:
//                if(tapX > 0 && tapY > 0){
                    switch(guideIndex){
                        case 1:
                            if(isTouch_inArea(tempTapX,tempTapY,51+177,290+136,51+177+91,290+136+44)){
                                //开始指导
                                changeState(2);
                                //清理
                                deadScreenShot = null;
                            }
                            else if(isTouch_inArea(tempTapX,tempTapY,51+303,290+136,51+303+91,290+136+44)){
                                //重新开始
                                isNewPlayer = false;
                                needGuide = false;
                                changeState(2);
                                initGame();
                                //清理
                                deadScreenShot = null;
                            }
                            break;
                        case 2:
                            if(isTouch_inArea(tempTapX,tempTapY,51+177,290+136,51+177+91,290+136+44)){
                                //防止用户自杀 赠送护盾
                                myPlane.creatShield();
                                //释放技能道具给用户
                                var tempCenterX = SW>>1;
                                var tempCenterY = SH>>1;
                                //护盾
                                creatOneTool(4, tempCenterX, tempCenterY);
                                //升级
                                creatOneTool(1, tempCenterX-50, tempCenterY-50);
                                creatOneTool(1, tempCenterX+50, tempCenterY+50);
                                creatOneTool(1, tempCenterX-100, tempCenterY);
                                creatOneTool(1, tempCenterX+100, tempCenterY-50);
//                            creatOneTool(1, tempCenterX-150, tempCenterY+50);
//                            creatOneTool(1, tempCenterX+150, tempCenterY);
                                changeState(2);
                                //清理
                                deadScreenShot = null;
                            }
                            else if(isTouch_inArea(tempTapX,tempTapY,51+303,290+136,51+303+91,290+136+44)){
                                //重新开始
                                isNewPlayer = false;
                                needGuide = false;
                                changeState(2);
                                initGame();
                                //清理
                                deadScreenShot = null;
                            }
                            break;

                        case 3:
                            if(isTouch_inArea(tempTapX,tempTapY,20,550-76-20,20+60,550-76-20+60)){
                                //冲刺
                                if(!myPlane.hasThunder && !isBossComing){
                                    creatThunder();
                                }
                                changeState(2);
                                //清理
                                deadScreenShot = null;
                            }
                            break;

                        case 4:
                            if(isTouch_inArea(tempTapX,tempTapY,20,550,20+60,550+60)){
                                //炸弹
                                if(!isKickBomb){
                                    creatBomb();
                                }
                                //防止用户自杀 赠送护盾
                                myPlane.creatShield();
                                changeState(2);
                                //清理
                                deadScreenShot = null;
                            }
                            break;

                        case 5:
                            if(isTouch_inArea(tempTapX,tempTapY,51+177,290+136,51+177+91,290+136+44)){
                                //再来一遍
                                changeState(2);
                                initGame();
                                //清理
                                deadScreenShot = null;
                            }
                            else if(isTouch_inArea(tempTapX,tempTapY,51+303,290+136,51+303+91,290+136+44)){
                                //重新开始
                                isNewPlayer = false;
                                needGuide = false;
                                changeState(2);
                                initGame();
                                //清理
                                deadScreenShot = null;
                            }
                            break;
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 9:
                if(showLevelupMove){
                    tapX = -1;
                    tapY = -1;
                    return;
                }
//                if(tapX > 0 && tapY > 0){
//                    var tempTapX = (tapX-Dis_left)/gamescaleX;
//                    var tempTapY = (tapY-Dis_top)/gamescaleY;
                    if(isTouch_inArea(tempTapX,tempTapY, 153, 586, 153+203, 586+81)){
                        if(myMaterialArray[1] >= pLevelUpCostMateria && myCoin >= pLevelUpCostCoin && myDiamond >= pLevelUpCostDiamond){
                            showLevelupMove = true;
                            p_levelup_move = 0;
                        }
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, CoinBtnStartX, 12, CoinBtnStartX+33, 12+33)){
                        //充值商店界面-金币
                        chargeDialog.show(false);
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, DiamondBtnStartX, 12, DiamondBtnStartX+33, 12+33)){
                        //充值商店界面-钻石
                        chargeDialog.show(true);
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, 395, 586, 395+57, 586+54)){
                        changeState(6);
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 10:
                if(showLightEffect){
                    tapX = -1;
                    tapY = -1;
                    return;
                }
//                if(tapX > 0 && tapY > 0){
//                    var tempTapX = (tapX-Dis_left)/gamescaleX;
//                    var tempTapY = (tapY-Dis_top)/gamescaleY;

                    if(isTouch_inArea(tempTapX,tempTapY, 156, 667, 156+203, 667+81)){
                        if(animLevelArray[animSelectIndex] > 0){
                            //强化
                            if(checkMaterialEnough() && myCoin >= pLevelUpCostCoin && myDiamond >= pLevelUpCostDiamond){
                                //提升等级
                                showLightEffect = true;
                                changeAngle = 0;
                            }
                        }
                        else{
                            //解锁
                            if(myCoin >= animLockCostCoin && myDiamond >= animLockCostDiamond){
                                showLightEffect = true;
                                changeAngle = 0;
                            }
                        }
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, 90, 430, 170, 520)){
                        animSelectIndex--;
                        if(animSelectIndex<0){
                            animSelectIndex = 5;
                        }
                        if(animLevelArray[animSelectIndex] > 0){
                            checkPlaneLevelUpNeed(animLevelArray[animSelectIndex]);
                        }
                        else{
                            checkAnimLockCost();
                        }
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, 320, 430, 400, 520)){
                        animSelectIndex++;
                        if(animSelectIndex>5){
                            animSelectIndex = 0;
                        }
                        if(animLevelArray[animSelectIndex] > 0){
                            checkPlaneLevelUpNeed(animLevelArray[animSelectIndex]);
                        }
                        else{
                            checkAnimLockCost();
                        }
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, CoinBtnStartX, 12, CoinBtnStartX+33, 12+33)){
                        //充值商店界面-金币
                        chargeDialog.show(false);
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, DiamondBtnStartX, 12, DiamondBtnStartX+33, 12+33)){
                        //充值商店界面-钻石
                        chargeDialog.show(true);
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, 395, 667, 395+57, 667+54)){
                        changeState(6);
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 11:
                if(showLightEffect){
                    tapX = -1;
                    tapY = -1;
                    return;
                }
//                if(tapX > 0 && tapY > 0){
//                    var tempTapX = (tapX-Dis_left)/gamescaleX;
//                    var tempTapY = (tapY-Dis_top)/gamescaleY;

                    if(isTouch_inArea(tempTapX,tempTapY, 156, 666, 156+203, 666+81)){
                        if(isBlackStateUnlock){
                            //开始游戏
                            //道具购买
                            isGameHasArrivalLimit = true;
                            isStartFromBlackStage = true;
                            backState = 11;
                            changeState(7);
                        }
                        else{
                            //解锁
                            if(myCoin >= unlockBlackNeedCoins && myMaterialArray[0] >= unlockBlackNeedKeys){
                                showLightEffect = true;
                                changeAngle = 0;
                            }
                        }
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, CoinBtnStartX, 12, CoinBtnStartX+33, 12+33)){
                        //充值商店界面-金币
                        chargeDialog.show(false);
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, DiamondBtnStartX, 12, DiamondBtnStartX+33, 12+33)){
                        //充值商店界面-钻石
                        chargeDialog.show(true);
                    }
                    else if(isTouch_inArea(tempTapX,tempTapY, 395, 672, 395+57, 672+54)){
                        //返回
                        changeState(12);
                    }
//                    tapX = -1;
//                    tapY = -1;
//                }
                break;

            case 12:
                //mission
                if(isTouch_inArea(tempTapX,tempTapY,202, 698, 202+158, 698+58)){
                    //boss模式
                    updateBossUnlock();
                    changeState(11);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, CoinBtnStartX, 12, CoinBtnStartX+33, 12+33)){
                    //充值商店界面-金币
                    chargeDialog.show(false);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, DiamondBtnStartX, 12, DiamondBtnStartX+33, 12+33)){
                    //充值商店界面-钻石
                    chargeDialog.show(true);
                }
                else if(isTouch_inArea(tempTapX,tempTapY, 395, 698, 395+57, 698+54)){
                    //返回
                    changeState(6);
                }
                else{
                    for(var i=0;i<15;i++){
                        if(isTouch_inArea(tempTapX,tempTapY,MissionPosXY[i][0],MissionPosXY[i][1],MissionPosXY[i][0]+55,MissionPosXY[i][1]+56)){
                            if(i == MissionSelectIndex){
                                //开始游戏
                                isGameHasArrivalLimit = true;
                                isStartFromBlackStage = false;
                                startMissionIndex = MissionSelectIndex;
                                backState = 12;
                                changeState(7);
                            }
                            else{
                                if(MissionUnlock[i]){
                                    MissionSelectIndex = i;
                                    toast4JS.buildToast("再点一次开始闯关",1000);
                                }
                            }
                            break;
                        }
                    }
                }
                break;
        }
        tapX = -1;
        tapY = -1;
    }
}

function logic(){
    switch (state){
        case 0:
            changeAngle = changeAngle + Math.PI/24;
            if(changeAngle >= 2*Math.PI){
                changeAngle = 0;
            }
            if(loadState == -1){
                if(loadCount == loadTotal){
                    //加载QQ空间所需图片完毕,开始播放动画，加载其他图片
                    loadState++;
                    loadCount = 0;
                    loadTotal = 0;
                }
            }
            else if(loadState == 0){
                loadResource();
                loadState++;
            }
            else if(loadState == 1){
                firstInitGame();
                loadState++;
            }
            else if(loadState == 2){
                if(loadCount == loadTotal){
                    changeState(1);
                    //清理加载用的数据
                    loadState = null;
//                    load_drawStartX = null;
//                    load_drawStartY = null;
//                    load_colorArray = null;
//                    load_basecolorSum = null;
                    load_banjing = null;
                    load_sradius = null;
                    load_currentIndex = null;
                    load_baseIndex = null;

                    loadGameOK = true;

                    circleRoundXYArray.splice(0, circleRoundXYArray.length);
                    setCircleRoundXYArray(80);
                }
            }
            break;

        case 1:
            effectCounter();
            cloudMoveCount = cloudMoveCount + 2;
            if(cloudMoveCount > SH){
                cloudMoveCount = cloudMoveCount - SH;
            }
            break;

        case 6:
            effectCounter();
            break;

        case 2:
            effectCounter();
            if(needGuide){
                guideCount++;
                if(guideCount == guideCountArray[guideIndex]){
                    guideIndex++;
                    deadScreenShot = canvas.getImageData(0,0,SW,SH);
                    changeState(5);
                }
            }

            if(isDead){
                deadCount--;
                if(deadCount == 0){
                    isDead = false;
                    deadScreenShot = canvas.getImageData(0,0,SW,SH);
                    changeState(3);
                    //更新复活所需的钻石数量
                    restartCostDiamond = (restartTimes+1)*20;
                }
            }
            if(isMyPlaneInvincible){
                MyPlaneInvincibleCount--;
                if(MyPlaneInvincibleCount == 0){
                    isMyPlaneInvincible = false;
                }
            }
            if(isKickBomb){
                KickBombCount--;
                if(KickBombCount == 0){
                    isKickBomb = false;
                    //炸弹清屏
                    var planesArraySize = planesArray.length;
                    for(var i1 = 0; i1 < planesArraySize; i1++){
                        var tempArrayPlanes = planesArray[i1];
                        for(var j1 = 0; j1 < tempArrayPlanes.length; j1++){
                            var enermy = tempArrayPlanes[j1];
                            if(enermy.visible){
                                enermy.beenHurt(1000);
                            }
                        }
                    }

                    var enermyBulletSize = bulletArray.length;
                    for(var i = 0; i < enermyBulletSize; i++){
                        var temp = bulletArray[i];
                        if(temp.visible){
                            temp.visible = false;
                        }
                    }
                }
            }

            mapMoveCount = mapMoveCount + movement;
            if(mapMoveCount > SH){
                mapMoveCount = mapMoveCount - SH;
            }
            if(!isBossComing && (!isGameHasArrivalLimit || arrivalIndex < targetArrivalIndex)){
                mapMoveTotal = mapMoveTotal + movement;
                if(mapMoveTotal > Math.MY_MAX_VALUE){
                    mapMoveTotal = mapMoveTotal - Math.MY_MAX_VALUE;
                }
            }
            if(nextArrivalDis <= mapMoveTotal){
                arrivalIndex++;
                //初始化即将出现的一波飞机
                initOneArrial();
                //计算下一波飞机到达距离
                nextArrivalDis = nextArrivalDis + 300;
                if(nextArrivalDis > Math.MY_MAX_VALUE){
                    nextArrivalDis = nextArrivalDis - Math.MY_MAX_VALUE;
                }
            }
            if(isStartCheckWin){
                winScreenCount++;
                if(winScreenCount > 20){
                    isStartCheckWin = false;
                    changingMap = true;
                    changingAlphaCount = 0;
                    isChangingAlphaCountIncrease = true;
                    needGotoWinScreen = true;
                }
            }
            if(changingMap){
                if(effect_count%2==0){
                    if(isChangingAlphaCountIncrease){
                        changingAlphaCount = changingAlphaCount+0.1;
                        if(changingAlphaCount >= 1){
                            changingAlphaCount = 1;
                            isChangingAlphaCountIncrease = false;
                            currentMapIndex++;
                            if(currentMapIndex >= 4){
                                currentMapIndex = 0;
                            }
                        }
                    }
                    else{
                        changingAlphaCount = changingAlphaCount-0.1;
                        if(changingAlphaCount <= 0){
                            changingAlphaCount = 0;
                            changingMap = false;
                            if(needGotoWinScreen){
                                needGotoWinScreen = false;

								hasUseDoubleMoney = false;

                                totalGotCoin = 0;
                                var numSum;
                                var tempGotArray;

                                if(isStartFromBlackStage){
                                    numSum = 5*currentBlackStage;
                                    //更新黑暗等级
                                    currentBlackStage = currentBlackStage + 1;
                                    isBlackStateUnlock = false;
                                }
                                else{
                                    numSum = startMissionIndex+2;
                                    if(startMissionIndex%3==2){
                                        numSum = numSum +2;
                                    }
                                    //更新关卡解锁
                                    if(startMissionIndex < 14 && !MissionUnlock[startMissionIndex+1]){
                                        MissionUnlock[startMissionIndex+1] = true;
                                    }
                                }
                                //残骸
                                tempGotArray = new Array();
                                tempGotArray.push(1);
                                tempGotArray.push(numSum);
                                rewardToolArray.push(tempGotArray);
                                //随机宠物材料
                                var totalTypeArray = [2,3,4,5];
                                var tempTypeIndex = randomNextInt(0,3);
                                tempGotArray = new Array();

                                tempGotArray.push(totalTypeArray[tempTypeIndex]);
                                tempGotArray.push(numSum);
                                rewardToolArray.push(tempGotArray);
                                //随机宠物材料
                                totalTypeArray.splice(tempTypeIndex,1);
                                tempTypeIndex = randomNextInt(0,2);
                                tempGotArray = new Array();

                                tempGotArray.push(totalTypeArray[tempTypeIndex]);
                                tempGotArray.push(numSum);
                                rewardToolArray.push(tempGotArray);
                                //随机宠物材料
                                totalTypeArray.splice(tempTypeIndex,1);
                                tempTypeIndex = randomNextInt(0,1);
                                tempGotArray = new Array();

                                tempGotArray.push(totalTypeArray[tempTypeIndex]);
                                tempGotArray.push(numSum);
                                rewardToolArray.push(tempGotArray);

                                //更新我的材料数组
                                myMaterialArray[1] = myMaterialArray[1] + numSum;
                                myMaterialArray[rewardToolArray[1][0]] = myMaterialArray[rewardToolArray[1][0]] + numSum;
                                myMaterialArray[rewardToolArray[2][0]] = myMaterialArray[rewardToolArray[2][0]] + numSum;
                                myMaterialArray[rewardToolArray[3][0]] = myMaterialArray[rewardToolArray[3][0]] + numSum;

                                if(currentMyEX > myMaxScore){
                                    myMaxScore = currentMyEX;
                                }

                                loading4JS.showLoading("正在提交请求，请等待！");
                                UpdateTotal(function(){
                                    loading4JS.closeLoading();
                                    deadScreenShot = canvas.getImageData(0,0,SW,SH);
                                    changeState(8);
                                });
                            }
                        }
                    }
                }
            }
            updateMyBulletsArray();
            updateMyPlane();
            updateBullets();
            updateEnermys();
            checkCollision();
            updateExplosions();
            updateTools();
            updateDiamends();
            levelupObject.update();
            warningObject.update();
            break;

        case 9:
            effectCounter();
            if(showLevelupMove){
                p_levelup_move = p_levelup_move + 3;
                if(p_levelup_move >= 80){
                    //提升等级
                    myPlaneLevel[driverIndex]++;
                    myFightAttack[driverIndex] = 5000 + (myPlaneLevel[driverIndex]-1)*500;

                    myMaterialArray[1] = myMaterialArray[1] - pLevelUpCostMateria;
                    myCoin = myCoin - pLevelUpCostCoin;
                    myDiamond = myDiamond - pLevelUpCostDiamond;
                    checkPlaneLevelUpNeed(myPlaneLevel[driverIndex]);
                    showLevelupMove = false;
                    p_levelup_move = 0;

                    loading4JS.showLoading("正在提交请求，请等待！");
                    UpdatePlaneLevel(function(){
                        loading4JS.closeLoading();
                        toast4JS.buildToast("战机升级完成！",1500);
                    });
                }
            }
            break;

        case 10:
            effectCounter();
            if(showLightEffect){
                changeAngle = changeAngle + Math.PI/12;
                if(changeAngle >= 2*Math.PI){
                    changeAngle = 0;
                    showLightEffect = false;

                    if(animLevelArray[animSelectIndex] > 0){
                        //强化
                        animLevelArray[animSelectIndex] = animLevelArray[animSelectIndex] + 1;
                        animAttackArray[animSelectIndex] = animAttackArray[animSelectIndex] + 1;
                        for(var i=2; i< 6; i++){
                            myMaterialArray[i] = myMaterialArray[i] - pLevelUpCostMateria;
                        }
                        myCoin = myCoin - pLevelUpCostCoin;
                        myDiamond = myDiamond - pLevelUpCostDiamond;
                        checkPlaneLevelUpNeed(animLevelArray[animSelectIndex]);
                    }
                    else{
                        //解锁
                        myCoin = myCoin - animLockCostCoin;
                        myDiamond = myDiamond - animLockCostDiamond;
                        animLevelArray[animSelectIndex] = 1;
                        animAttackArray[animSelectIndex] = animSelectIndex+1;
                        checkPlaneLevelUpNeed(animLevelArray[animSelectIndex]);
                    }

                    loading4JS.showLoading("正在提交请求，请等待！");
                    UpdatePetLevel(function(){
                        loading4JS.closeLoading();
                    });
                }
            }
            break;

        case 11:
            effectCounter();
            if(showLightEffect){
                changeAngle = changeAngle + Math.PI/12;
                if(changeAngle >= 2*Math.PI){
                    changeAngle = 0;
                    showLightEffect = false;

                    //解锁
                    isBlackStateUnlock = true;
                    //扣金币  钥匙
                    myCoin = myCoin - unlockBlackNeedCoins;
                    myMaterialArray[0] = myMaterialArray[0] - unlockBlackNeedKeys;

                    loading4JS.showLoading("正在提交请求，请等待！");
                    UpdateTotal(function(){
                        loading4JS.closeLoading();
                    });
                }
            }
            break;

        case 12:
            if(MissionSelectIndex>=0){
                changeAngle = changeAngle + Math.PI/12;
                if(changeAngle >= 2*Math.PI){
                    changeAngle = 0;
                }
            }
            break;
    }
}

function draw(){
    if(state == 0){
        canvas.fillStyle = "#f7f7f7";
        canvas.fillRect(0,0,SW,SH);

        if(loadState > -1){
            canvas.drawImage(img_startpage_icon, 166, 150);
//            canvas.drawImage(img_startpage_font1, 106, 370);
            canvas.drawImage(img_startpage_font2, 94, 600);

            canvas.drawImage(img_startpage_lower, 74, 630);
            var length = zhengchu(365*loadCount, loadTotal);
            canvas.drawImage(img_startpage_upper, 0, 0, length, 7, 74, 630, length, 7);

//            canvas.textAlign = "center";
//            canvas.textBaseline = "middle";
//            canvas.fillStyle = "#ffffff";
//            canvas.fillText(loadCount+"/"+loadTotal,load_drawStartX,load_drawStartY);

            var tempStarX = 74 + length-14;
            var tempStarY = 620;

            canvas.save();
            canvas.translate(tempStarX+14, tempStarY+13);
//            changeAngle = changeAngle + Math.PI/12;
//            if(changeAngle >= 2*Math.PI){
//                changeAngle = 0;
//            }
            canvas.rotate(changeAngle);
            canvas.drawImage(img_startpage_star, -14, -13);
            canvas.restore();
        }

//
//        circleRoundXYArray
//        for(var i=0; i< 24; i++){
//            drawRound(load_colorArray[i], load_drawStartX + circleRoundXYArray[i][0],load_drawStartY + circleRoundXYArray[i][1], load_sradius, 0, Math.PI*2, false);
//        }
    }
    else if(state == 1){
        canvas.drawImage(logo_bg,0,0);
        canvas.globalAlpha = 0.5;
        clipDrawFrame(black_cloud, 0, 0, 0, cloudMoveCount, SW, SH-cloudMoveCount, canvas);
        clipDrawFrame(black_cloud, 0, SH-cloudMoveCount, 0, 0, SW, cloudMoveCount, canvas);
        canvas.globalAlpha = 1;
        canvas.drawImage(logo_name,0,466);
        if(effect_count%10>5){
            canvas.drawImage(logo_opera,SW-339>>1,660);
        }
    }
    else if(state == 6){
        canvas.drawImage(select_bg, 0, 0);
        canvas.drawImage(imgs_driver_b[driverIndex], 167, 94);
        //宠物
        canvas.drawImage(img_select_anim, 10, 60);
        //排名
        canvas.drawImage(img_select_rank, 420, 142);
        //充值商店
        canvas.drawImage(img_select_store, 422, 60);
        //每日奖励
        canvas.drawImage(img_select_daily, 203, 628);
        //name
        canvas.drawImage(img_animname_bg, 178, 300);
        canvas.textAlign = "center";
        canvas.textBaseline = "top";
        canvas.fillStyle = "#ffffff";
        canvas.fillText(driverName[driverIndex], 256, 300+12);
//        canvas.drawImage(imgs_driver_n[driverIndex], 16, 300);
        //骰子
        if(!hasChooseNameArray[driverIndex]){
            canvas.drawImage(img_select_dice, 344, 300);
        }
        //金币。钻石
        drawCoinAndDiamond();
        //飞机
        canvas.drawImage(imgs_myPlane[driverIndex][2+(zhengchu(effect_count,2))%2], 190, 350);
        //点击战机升级
        canvas.drawImage(img_select_kick, 285, 340);
        //历史最高分
        drawNum(canvas, myMaxScore, 297, 492, 10, 13, img_num_small);
        //战斗力
        drawNum(canvas, myFightAttack[driverIndex], 284, 518, 10, 13, img_num_small);

        //购买  选择
        if(myPlaneLevel[driverIndex] > 0){
            if(driverIndex != fightDriverIndex){
                //选择出战
                canvas.drawImage(img_select_choose, 170, 540);
            }
        }
        else{
            //购买
            canvas.drawImage(img_select_buy_d, 170, 540);
            drawNum(canvas, BuyDriverCostArray[driverIndex], 170+60, 540+8, 21, 24, img_charge_font_b);
            //lock
            canvas.drawImage(img_anim_lock, 236, 200);
        }
//        //name
//        canvas.drawImage(img_planename, 180, 540);
//        //level star
//        var starNum = myPlaneLevel[driverIndex];
//        if(starNum>5){
//            starNum = 5;
//        }
//        var starStartx = 257-33*starNum/2;
//        for(var i=0; i< starNum; i++){
//            canvas.drawImage(img_planestar, starStartx, 582);
//            starStartx = starStartx + 33;
//        }
    }
    else if(state == 7){
        canvas.drawImage(tool_bg,0,0);
        drawCoinAndDiamond();
        canvas.drawImage(imgs_driver_s[fightDriverIndex], 2, 42);
//        canvas.drawImage(imgs_driver_sn[driverIndex], 18, 95);
        drawNum(canvas, ToolCostArray[0], 408, 232, 10, 13, img_num_small);
        drawNum(canvas, ToolCostArray[1], 408, 313, 10, 13, img_num_small);
        drawNum(canvas, ToolCostArray[2], 408, 394, 10, 13, img_num_small);
        drawNum(canvas, ToolCostArray[3], 408, 475, 10, 13, img_num_small);


        canvas.textAlign = "center";
        canvas.textBaseline = "top";
        canvas.fillStyle = "#000000";
        canvas.fillText(myToolArray[0], 58, 254);
        canvas.fillText(myToolArray[1], 58, 334);
        canvas.fillText(myToolArray[2], 58, 415);
        canvas.fillText(myToolArray[3], 58, 497);
    }
    else if(state == 2){
        //背景
        if(isStartFromBlackStage){
            canvas.fillStyle = "#000000";
            canvas.fillRect(0,0,SW,SH);
        }
        else{
            clipDrawFrame(game_bg_array[currentMapIndex], 0, 0, 0, mapMoveCount, SW, SH-mapMoveCount, canvas);
            clipDrawFrame(game_bg_array[currentMapIndex], 0, SH-mapMoveCount, 0, 0, SW, mapMoveCount, canvas);
        }
        if(changingMap){
            canvas.globalAlpha = changingAlphaCount;
            canvas.drawImage(black_bg,0,0);
            canvas.globalAlpha = 1;
            if(isGameHasArrivalLimit){
                canvas.drawImage(img_font_gamewin, 59, 300);
            }
        }
        //道具按钮
        for(var i=0;i<3;i++){
            canvas.drawImage(img_btntools[i],20,550-(76+20)*i);
            drawNum(canvas, myToolArray[1+i], 20 + 60 -5,550-(76+20)*i+76-15, 10, 13, img_num_small);
        }
        //pause
        canvas.drawImage(img_btnPause,SW-54-10,35);
        //声音
        if(AUDIO){
            if(SoundEnable){
                canvas.drawImage(img_btnSound,SW-54-10,35+54+25);
            }
            else{
                canvas.drawImage(img_btnNoSound,SW-54-10,35+54+25);
            }
        }
        //画分数
        drawNum(canvas, currentMyEX, 260 ,5,27,27,scoreImg);
//        //画等级
        var textStartY = 5;
//        canvas.drawImage(font_lv,5,textStartY);
//        canvas.drawImage(img_bar_experience, 0,0,200*currentMyEX/levelTotalEX,16, 40,textStartY-1,200*currentMyEX/levelTotalEX,16);
//        canvas.drawImage(img_bar_frame, 40,textStartY-1);
//        canvas.textAlign = "center";
//        canvas.textBaseline = "top";
//        canvas.fillStyle = "#000000";
//        canvas.fillText(currentMyLevel,140,textStartY);
        //画hp
//        textStartY = textStartY + 15 +8;
        canvas.drawImage(font_hp,5,textStartY);
        canvas.drawImage(img_bar_blood, 0,0,200*currentMyHP/levelTotalHP,16, 40,textStartY-1,200*currentMyHP/levelTotalHP,16);
        canvas.drawImage(img_bar_frame, 40,textStartY-1);
        canvas.textAlign = "center";
        canvas.textBaseline = "top";
        canvas.fillStyle = "#000000";
        canvas.fillText(currentMyHP,140,textStartY);

        drawEnermys();
        drawMyPlane();
        drawBullets();
        drawMyBulletsArray();
        drawTools();
        drawDownDiamond();
        drawExplosions();
        levelupObject.paint(canvas);
        warningObject.paint(canvas);

        if(isScreenRed){
            canvas.globalAlpha = screenRedCount/10;
            canvas.drawImage(red_bg,0,0);
            canvas.globalAlpha = 1;
            screenRedCount--;
            if(screenRedCount == 0){
                isScreenRed = false;
            }
        }
    }
    else if(state == 3){
        canvas.putImageData(deadScreenShot,0,0);
        canvas.drawImage(black_alpha,0,0);
        canvas.drawImage(img_font_gameover, 51,290);

        canvas.font = "20px Arial";
        canvas.textAlign = "left";
        canvas.textBaseline = "top";
        canvas.fillStyle = "#00ffff";
        canvas.fillText(restartCostDiamond, 51+128, 290+189);
        canvas.font = "14px Arial";
    }
    else if(state == 8){
        canvas.putImageData(deadScreenShot,0,0);
        canvas.drawImage(black_alpha,0,0);
        canvas.drawImage(img_got_bg, 16, 150);
        //光效果
        canvas.save();
        canvas.translate(157+100, 223+100);
        changeAngle = changeAngle + Math.PI/12;
        if(changeAngle >= 2*Math.PI){
            changeAngle = 0;
        }
        canvas.rotate(changeAngle);
        canvas.drawImage(img_effect_light, -100, -100);
        canvas.restore();
        canvas.drawImage(img_got_btngo, 192, 648);
        //star
        var starNum = zhengchu(arrivalIndex-1, 20) + 1;
        if(starNum>5){
            starNum = 5;
        }
        var tempStartx = (SW-33*starNum)>>1;
        for(var i=0; i< starNum; i++){
            canvas.drawImage(img_planestar, tempStartx, 258);
            tempStartx = tempStartx + 33;
        }
        //画分数
        drawNum(canvas, currentMyEX, SW-checkNumLength(currentMyEX, 43)>>1, 305, 43, 58, img_num_xxbig);
        //金币获得
        drawNum(canvas, totalGotCoin, 290, 384, 10, 13, img_num_small);
        if(hasUseDoubleMoney){
            canvas.textAlign = "left";
            canvas.textBaseline = "top";
            canvas.fillStyle = "#ffffff";
            canvas.fillText( "（双倍金币效果）", 290+checkNumLength(totalGotCoin, 10), 382);
        }
        //历史最高分
        drawNum(canvas, myMaxScore, 265, 416, 10, 13, img_num_small);
        //获得奖励
        if(rewardToolArray[2][0] > 0){
            tempStartx = SW-84*4-10*3>>1;
            for(var i=0; i< 4; i++){
                canvas.drawImage(imgs_got_tool[rewardToolArray[i][0]], tempStartx, 477);
                drawNum(canvas, rewardToolArray[i][1], tempStartx+74 - checkNumLength(rewardToolArray[i][1], 15), 487, 15, 16, img_num_money);
                tempStartx = tempStartx + 84 + 10;
            }
        }
    }
    else if(state == 4){
        canvas.putImageData(deadScreenShot,0,0);
        canvas.drawImage(black_alpha,0,0);
        canvas.drawImage(img_font_gamepause, 51,290);
    }
    else if(state == 5){
        switch(guideIndex){
            case 1:
                canvas.putImageData(deadScreenShot,0,0);
                canvas.drawImage(black_alpha,0,0);
                canvas.drawImage(imgs_guide[0], 51,290);
                break;

            case 2:
                canvas.putImageData(deadScreenShot,0,0);
                canvas.drawImage(black_alpha,0,0);
                canvas.drawImage(imgs_guide[1], 51,290);
                break;

            case 3:
                canvas.putImageData(deadScreenShot,0,0);
                canvas.drawImage(black_alpha,0,0);
                canvas.drawImage(img_btntools[1],20,550-(76+20));
                canvas.drawImage(imgs_guide[2], 20+60+5,550-(76+20));
                break;

            case 4:
                canvas.putImageData(deadScreenShot,0,0);
                canvas.drawImage(black_alpha,0,0);
                canvas.drawImage(img_btntools[0],20,550);
                canvas.drawImage(imgs_guide[3], 20+60+5,550);
                break;

            case 5:
                canvas.putImageData(deadScreenShot,0,0);
                canvas.drawImage(black_alpha,0,0);
                canvas.drawImage(imgs_guide[4], 51,290);
                break;
        }
    }
    else if(state == 9){
        canvas.fillStyle = "#000000";
        canvas.fillRect(0,0,SW,SH);
        drawCoinAndDiamond();
        canvas.drawImage(img_btn_back, 395, 586);
        var tempBGStartX = 16;
        var tempBGStartY = 105;
        canvas.drawImage(img_p_levelup_bg, tempBGStartX, tempBGStartY);
        //飞机
        canvas.drawImage(imgs_myPlane[driverIndex][2+(zhengchu(effect_count,2))%2], tempBGStartX+58, tempBGStartY+89);
        //移动的罩子
        if(showLevelupMove){
            canvas.drawImage(img_p_levelup_move, tempBGStartX+57, tempBGStartY+69+p_levelup_move);
        }
        //等级
        drawNum(canvas, myPlaneLevel[driverIndex], tempBGStartX+119, tempBGStartY+229, 10, 13, img_num_small);
        drawNum(canvas, myPlaneLevel[driverIndex], tempBGStartX+323, tempBGStartY+126, 10, 13, img_num_small);
        drawNum(canvas, myPlaneLevel[driverIndex]+1, tempBGStartX+398, tempBGStartY+125, 9, 13, img_num_levelup);
        //战斗力
        drawNum(canvas, myFightAttack[driverIndex], tempBGStartX+320, tempBGStartY+149, 10, 13, img_num_small);
        drawNum(canvas, myFightAttack[driverIndex]+500, tempBGStartX+398, tempBGStartY+148, 9, 13, img_num_levelup);
        //攻击力+
        drawNum(canvas, myPlaneLevel[driverIndex]-1, tempBGStartX+336, tempBGStartY+171, 10, 13, img_num_small);
        drawNum(canvas, myPlaneLevel[driverIndex], tempBGStartX+398, tempBGStartY+171, 9, 13, img_num_levelup);
        //消耗
        drawNum(canvas, pLevelUpCostMateria, tempBGStartX+296, tempBGStartY+245, 9, 13, img_num_levelup);
        drawNum(canvas, pLevelUpCostCoin, tempBGStartX+397, tempBGStartY+227, 9, 13, img_num_levelup);
        drawNum(canvas, pLevelUpCostDiamond, tempBGStartX+397, tempBGStartY+268, 9, 13, img_num_levelup);

        canvas.textAlign = "center";
        canvas.textBaseline = "top";
        canvas.fillStyle = "#ffffff";
        canvas.fillText("我的残骸数量："+myMaterialArray[1],SW>>1,460);

        canvas.drawImage(imgs_got_tool[1], 213, 490);
        drawNum(canvas, myMaterialArray[1], 213+74 - checkNumLength(myMaterialArray[1], 15), 500, 15, 16, img_num_money);

        if(myMaterialArray[1] >= pLevelUpCostMateria && myCoin >= pLevelUpCostCoin && myDiamond >= pLevelUpCostDiamond){
            if(showLevelupMove){
                canvas.drawImage( img_btn_strang_no, 153, 586);
            }
            else{
                canvas.drawImage( img_btn_strang_yes, 153, 586);
            }
        }
        else{
            canvas.drawImage( img_btn_strang_no, 153, 586);
            if(myMaterialArray[1] < pLevelUpCostMateria){
                canvas.fillStyle = "#ff0000";
                canvas.fillText("残骸不足",SW>>1,675);
            }
            else if(myCoin < pLevelUpCostCoin){
                canvas.fillStyle = "#ff0000";
                canvas.fillText("金币不足",SW>>1,675);
            }
            else if(myDiamond < pLevelUpCostDiamond){
                canvas.fillStyle = "#ff0000";
                canvas.fillText("钻石不足",SW>>1,675);
            }
        }
    }
    else if(state == 10){
        canvas.drawImage( img_anim_center_bg, 0, 0);
        drawCoinAndDiamond();
        canvas.drawImage(imgs_driver_b[fightDriverIndex], 36, 160);
//        canvas.drawImage(imgs_driver_sn[driverIndex], 82, 368);
        //name
        canvas.drawImage(img_animname_bg, 271, 113);
        canvas.textAlign = "center";
        canvas.textBaseline = "top";
        canvas.fillStyle = "#f9cb15";
        canvas.fillText(animName[animSelectIndex], 348, 126);

        canvas.drawImage(img_btn_back, 395, 667);

        if(animLevelArray[animSelectIndex] > 0){
            //等级
            canvas.textAlign = "left";
            canvas.fillText(animLevelArray[animSelectIndex], 326, 157);
            //skill
            canvas.fillText(animSkillName[animSelectIndex], 326, 179);
            //攻击力
            canvas.fillText("+"+animAttackArray[animSelectIndex], 344, 203);
            //LV2->lv3
            canvas.fillText("LV "+animLevelArray[animSelectIndex], 303, 224);
            canvas.fillStyle = "#05d1fd";
            canvas.fillText("LV "+(animLevelArray[animSelectIndex] + 1), 408, 224);
            //所需数量
            drawNum(canvas, pLevelUpCostMateria, 400, 245, 9, 13, img_num_levelup);
            drawNum(canvas, pLevelUpCostMateria, 400, 266, 9, 13, img_num_levelup);
            drawNum(canvas, pLevelUpCostMateria, 400, 286, 9, 13, img_num_levelup);
            drawNum(canvas, pLevelUpCostMateria, 400, 306, 9, 13, img_num_levelup);
            drawNum(canvas, pLevelUpCostCoin, 400, 326, 9, 13, img_num_levelup);
            drawNum(canvas, pLevelUpCostDiamond, 400, 346, 9, 13, img_num_levelup);
            //宠物
            if(showLightEffect){
                canvas.save();
                canvas.translate(147+100, 377+100);
                canvas.rotate(changeAngle);
                canvas.drawImage(img_effect_light, -100, -100);
                canvas.restore();
            }

            canvas.drawImage(imgs_anim_b[animSelectIndex], 199, 426);
            //材料
            canvas.drawImage(img_font_cur_material, 176, 550);
            //材料
            var tempStartx = SW-84*4-10*3>>1;
            for(var i=2; i< 6; i++){
                canvas.drawImage(imgs_got_tool[i], tempStartx, 572);
                drawNum(canvas, myMaterialArray[i], tempStartx+74 - checkNumLength(myMaterialArray[i], 15), 582, 15, 16, img_num_money);
                tempStartx = tempStartx + 84 + 10;
            }
            //btn
            if(checkMaterialEnough() && myCoin >= pLevelUpCostCoin && myDiamond >= pLevelUpCostDiamond){
                if(showLightEffect){
                    canvas.drawImage( img_btn_strang_no, 156, 667);
                }
                else{
                    canvas.drawImage( img_btn_strang_yes, 156, 667);
                }
            }
            else{
                canvas.drawImage( img_btn_strang_no, 156, 667);
                canvas.textAlign = "center";
                if(!checkMaterialEnough()){
                    canvas.fillStyle = "#ff0000";
                    canvas.fillText("材料不足",SW>>1,746);
                }
                else if(myCoin < pLevelUpCostCoin){
                    canvas.fillStyle = "#ff0000";
                    canvas.fillText("金币不足",SW>>1,746);
                }
                else if(myDiamond < pLevelUpCostDiamond){
                    canvas.fillStyle = "#ff0000";
                    canvas.fillText("钻石不足",SW>>1,746);
                }
            }
        }
        else{
            //等级
            canvas.textAlign = "left";
            canvas.fillText("???", 326, 157);
            //skill
            canvas.fillText("???", 326, 179);
            //攻击力
            canvas.fillText("???", 344, 203);
            //LV2->lv3
            canvas.fillText("???", 303, 224);
            canvas.fillStyle = "#05d1fd";
            canvas.fillText("???", 408, 224);
            //所需数量
            canvas.fillText("???", 400, 245 -1);
            canvas.fillText("???", 400, 266 -1);
            canvas.fillText("???", 400, 286 -1);
            canvas.fillText("???", 400, 306 -1);
            canvas.fillText("???", 400, 326 -1);
            canvas.fillText("???", 400, 346 -1);
            //宠物
            if(showLightEffect){
                canvas.save();
                canvas.translate(147+100, 377+100);
                canvas.rotate(changeAngle);
                canvas.drawImage(img_effect_light, -100, -100);
                canvas.restore();
            }

            canvas.drawImage(imgs_anim_egg[zhengchu(effect_count, 5)%2], 199, 426);
            canvas.drawImage(img_anim_lock, 221, 458);
            canvas.drawImage(img_font_unlock, 192, 540);
            //解锁需消耗
            canvas.textAlign = "center";
            canvas.fillStyle = "#fcf305";
            if(animLockCostCoin>0){
                canvas.fillText("解锁需消耗"+animLockCostCoin+"金币", SW>>1, 612);
            }
            else{
                canvas.fillText("解锁需消耗"+animLockCostDiamond+"钻石", SW>>1, 612);
            }
            //btn
            if(myCoin >= animLockCostCoin && myDiamond >= animLockCostDiamond){
                if(showLightEffect){
                    canvas.drawImage( img_btn_lock_no, 156, 667);
                }
                else{
                    canvas.drawImage( img_btn_lock_yes, 156, 667);
                }
            }
            else{
                canvas.drawImage( img_btn_lock_no, 156, 667);
                canvas.textAlign = "center";
                if(myCoin < animLockCostCoin){
                    canvas.fillStyle = "#ff0000";
                    canvas.fillText("金币不足",SW>>1,746);
                }
                else if(myDiamond < animLockCostDiamond){
                    canvas.fillStyle = "#ff0000";
                    canvas.fillText("钻石不足",SW>>1,746);
                }
            }
        }
    }
    else if(state == 11){
        canvas.fillStyle = "#000000";
        canvas.fillRect(0,0,SW,SH);
        drawCoinAndDiamond();
        canvas.drawImage(imgs_driver_s[fightDriverIndex], 2, 42);
//        canvas.drawImage(imgs_driver_sn[driverIndex], 18, 95);

        canvas.drawImage(img_blackhole_bg, 0, 148-20);
        //升级效果
        if(showLightEffect){
            canvas.save();
            canvas.translate(147+100, 237+100);
            canvas.rotate(changeAngle);
            canvas.drawImage(img_effect_light, -100, -100);
            canvas.restore();
        }
        //stage
        canvas.textAlign = "center";
        canvas.textBaseline = "top";
        canvas.fillStyle = "#f9cb15";
        canvas.fillText("暗黑世界地下"+currentBlackStage+"层", SW>>1, 495-20);
        //back
        canvas.drawImage(img_btn_back, 395, 672);
        //钥匙数量
        canvas.fillStyle = "#ffffff";
        canvas.fillText("当前我的钥匙数量："+myMaterialArray[0], SW>>1, 558);
        canvas.drawImage(imgs_got_tool[0], 213, 578);
        drawNum(canvas, myMaterialArray[0], 213+74 - checkNumLength(myMaterialArray[0], 15), 588, 15, 16, img_num_money);

        if(isBlackStateUnlock){
            canvas.drawImage( img_btn_start_yes, 156, 666);
        }
        else{
            canvas.fillStyle = "#fcf305";
            canvas.fillText("解锁需消耗银河钥匙："+unlockBlackNeedKeys+"，金币："+unlockBlackNeedCoins, SW>>1, 530);
            //btn
            if(myCoin >= unlockBlackNeedCoins && myMaterialArray[0] >= unlockBlackNeedKeys){
                if(showLightEffect){
                    canvas.drawImage( img_btn_lock_no, 156, 666);
                }
                else{
                    canvas.drawImage( img_btn_lock_yes, 156, 666);
                }
            }
            else{
                canvas.drawImage( img_btn_lock_no, 156, 666);
                if(myCoin < unlockBlackNeedCoins){
                    canvas.fillStyle = "#ff0000";
                    canvas.fillText("金币不足",SW>>1,745);
                }
                else if(myMaterialArray[0] < unlockBlackNeedKeys){
                    canvas.fillStyle = "#ff0000";
                    canvas.fillText("钥匙不足",SW>>1,745);
                }
            }
        }
    }
    else if(state ==12){
        canvas.drawImage( img_mission_bg, 0, 0);
        drawCoinAndDiamond();
        for(var i=0;i<MissionUnlock.length;i++){
            if(MissionUnlock[i]){
                canvas.drawImage( img_mission_star, MissionPosXY[i][0], MissionPosXY[i][1]);
            }
            else{
                canvas.drawImage( img_mission_star_u, MissionPosXY[i][0], MissionPosXY[i][1]);
            }
        }
        if(MissionSelectIndex>=0){
            canvas.save();
            canvas.translate(MissionPosXY[MissionSelectIndex][0]-12+37, MissionPosXY[MissionSelectIndex][1]-13+37);
            canvas.rotate(changeAngle);
            canvas.drawImage(img_mission_select, -37, -37);
            canvas.restore();
        }
        //back
        canvas.drawImage(img_btn_back, 395, 698);
    }
    else if(state ==13){
        canvas.putImageData(deadScreenShot,0,0);
        canvas.drawImage(black_alpha,0,0);
    }
}

function updateMissionSelectIndex(){
    for(var i=0;i<MissionUnlock.length;i++){
        if(!MissionUnlock[i]){
            MissionSelectIndex = i-1;
            break;
        }
    }
}

function updateBossUnlock(){
    if(!isBlackStateUnlock){
        unlockBlackNeedKeys = 10*currentBlackStage;
        unlockBlackNeedCoins = 1000*(currentBlackStage + 1);
    }
}

function checkMaterialEnough(){
    for(var i=2; i< 6; i++){
        if(myMaterialArray[i] < pLevelUpCostMateria){
            return false;
        }
    }
    return true;
}

function checkAnimLockCost(){
    switch (animSelectIndex){
        case 1:
            animLockCostCoin = 3000;
            animLockCostDiamond = 0;
            break;

        case 2:
            animLockCostCoin = 5000;
            animLockCostDiamond = 0;
            break;

        case 3:
            animLockCostCoin = 7000;
            animLockCostDiamond = 0;
            break;

        case 4:
        animLockCostCoin = 0;
        animLockCostDiamond = 30;
        break;

        case 5:
            animLockCostCoin = 0;
            animLockCostDiamond = 50;
            break;
    }
}

function checkPlaneLevelUpNeed(checkLevel){
    if(checkLevel<6){
        pLevelUpCostDiamond = 0;
        if(checkLevel == 1){
            pLevelUpCostCoin = 1000;
        }
        else{
            pLevelUpCostCoin = 5000*(checkLevel-1);
        }
    }
    else{
        pLevelUpCostCoin = 0;
        pLevelUpCostDiamond = 10*Math.pow(2,checkLevel-5);
    }
    pLevelUpCostMateria = 10+ 10*zhengchu(checkLevel*(checkLevel-1), 2);//升级所需的残骸
}

function updateEnermys(){
    var size = planesArray.length;
    var releaseArray = new Array();
    for(var i = 0; i < size; i++){
        var tempArray = planesArray[i];
        var unusableCount = 0;
        for(var j = 0; j < tempArray.length; j++){
            var enermy = tempArray[j];
            if(enermy.usable){
                enermy.update();
            }
            if(!enermy.usable){
                unusableCount++;
            }
        }
        if(unusableCount == tempArray.length){
            releaseArray.push(i);
        }
    }
    for(var i = 0; i < releaseArray.length; i++){
        var remove = planesArray.splice(releaseArray[i]-i,1);
        remove = null;
    }
    if(!hasWinGameMission && isGameHasArrivalLimit && arrivalIndex == targetArrivalIndex && planesArray.length == 0){
        hasWinGameMission = true;
        isStartCheckWin = true;
    }
}

function updateExplosions(){
    var size = explosionsArray.length;
    var releaseArray = new Array();
    for(var i = 0; i < size; i++){
        var temp = explosionsArray[i];
        temp.update();
        if(!temp.usable){
            releaseArray.push(i);
        }
    }
    for(var i = 0; i < releaseArray.length; i++){
        var remove = explosionsArray.splice(releaseArray[i]-i,1);
        remove = null;
    }
}

function updateTools(){
    var size = SkillToolArray.length;
    var releaseArray = new Array();
    for(var i = 0; i < size; i++){
        var temp = SkillToolArray[i];
        if(temp.visible){
            temp.updatePos();
        }
        if(!temp.visible){
            releaseArray.push(i);
        }
    }
    for(var i = 0; i < releaseArray.length; i++){
        var remove = SkillToolArray.splice(releaseArray[i]-i,1);
        remove = null;
    }
}

function updateDiamends(){
    var size = DownDiamendArray.length;
    var releaseArray = new Array();
    for(var i = 0; i < size; i++){
        var temp = DownDiamendArray[i];
        if(temp.visible){
            temp.update();
        }
        if(!temp.visible){
            releaseArray.push(i);
        }
    }
    for(var i = 0; i < releaseArray.length; i++){
        var remove = DownDiamendArray.splice(releaseArray[i]-i,1);
        remove = null;
    }
}

function updateBullets(){
    var size = bulletArray.length;
    for(var i = 0; i < size; i++){
        var temp = bulletArray[i];
        temp.updatePos();
    }
}

function updateMyBulletsArray(){
    var size = myBulletsArray.length;
    var releaseArray = new Array();
    for(var i = 0; i < size; i++){
//        var tempArray = myBulletsArray[i];
//        var unusableCount = 0;
//        for(var j = 0; j < tempArray.length; j++){
//            var temp = tempArray[j];
//            temp.updatePos();
//            if(!temp.visible){
//                unusableCount++;
//            }
//        }
//        if(unusableCount == tempArray.length){
//            releaseArray.push(i);
//        }
        var temp = myBulletsArray[i];
        temp.updatePos();
        if(!temp.visible){
            releaseArray.push(i);
        }
    }
    for(var i = 0; i < releaseArray.length; i++){
        var remove = myBulletsArray.splice(releaseArray[i]-i,1);
        remove = null;
    }
}

function checkCollision(){
    //我的子弹和敌机
    var myBulletsArraySize = myBulletsArray.length;
    for(var i = 0; i < myBulletsArraySize; i++){
//        var tempArray = myBulletsArray[i];
//        for(var j = 0; j < tempArray.length; j++){
//            var temp = tempArray[j];
            var temp = myBulletsArray[i];
            if(temp.visible){
                var planesArraySize = planesArray.length;
                ergodicPlanesArray:
                for(var i1 = 0; i1 < planesArraySize; i1++){
                    var tempArrayPlanes = planesArray[i1];
                    for(var j1 = 0; j1 < tempArrayPlanes.length; j1++){
                        var enermy = tempArrayPlanes[j1];
                        if(enermy.visible){
                            if(rectCollisionCheck(temp.posX,temp.posY,temp.width,temp.height, enermy.posX,enermy.posY,enermy.width,enermy.height)){
                                temp.visible = false;
                                enermy.beenHurt(temp.attack);

                                //子弹爆炸效果
                                var tempEffect = new ExplosionEffect();
                                tempEffect.InitExplosionEffect(temp.posX+temp.width/2, temp.posY, 10, 10, imgs_explosion[2],3, 1, 0,true);
                                explosionsArray.push(tempEffect);

                                break ergodicPlanesArray;
                            }
                        }
                    }
                }
            }
//        }
    }
    if(myPlane.visiable){
        if(!isMyPlaneInvincible){
            //敌机子弹和我
            var enermyBulletSize = bulletArray.length;
            for(var i = 0; i < enermyBulletSize; i++){
                var temp = bulletArray[i];
                if(temp.visible){
                    if(myPlane.hasShield || myPlane.hasThunder){
                        //和防护罩碰撞
                        if(rectCollisionCheck(temp.posX,temp.posY,temp.width,temp.height, myPlane.PlaneX+1,myPlane.PlaneY,122,122)){
                            temp.visible = false;
                        }
                    }
                    else{
                        //和中心碰撞
                        if(rectCollisionCheck(temp.posX,temp.posY,temp.width,temp.height, myPlane.PlaneX+57,myPlane.PlaneY+37, 10, 10)){
                            //子弹消失
                            temp.visible = false;
                            //飞机
                            currentMyHP = currentMyHP - temp.attack;
                            isScreenRed = true;
                            screenRedCount = 10;
                            //子弹等级降低
                            if(myPlane.attackType>0){
                                myPlane.attackType--;
                            }
                            //是否死亡
                            if(currentMyHP <= 0){
                                currentMyHP = 0;
                                //爆炸效果
                                var temp = new ExplosionEffect();
                                temp.InitExplosionEffect(myPlane.centerPosX, myPlane.centerPosY, 192, 192, imgs_explosion[0],7, 2, 0,false);
                                explosionsArray.push(temp);
                                //死亡
                                myPlane.visiable = false;
                                isDead = true;
                                deadCount = 15;

                                sfx("explode4");
                                break;
                            }
                            else{
                                changeMyPlaneInvincible();
                                break;
                            }
                        }
                    }
                }
            }
            //敌机和我
            if(myPlane.visiable && !isMyPlaneInvincible){
                var planesArraySize = planesArray.length;
                againergodicPlanesArray:
                    for(var i1 = 0; i1 < planesArraySize; i1++){
                        var tempArrayPlanes = planesArray[i1];
                        for(var j1 = 0; j1 < tempArrayPlanes.length; j1++){
                            var enermy = tempArrayPlanes[j1];
                            if(enermy.visible){
                                if(myPlane.hasShield || myPlane.hasThunder){
                                    //和防护罩碰撞
//                                    if(rectCollisionCheck(myPlane.PlaneX+1,myPlane.PlaneY,122,122,enermy.posX,enermy.posY,enermy.width,enermy.height)){
//                                    }
                                    if(myPlane.hasThunder){
                                        if(rectCollisionCheck(myPlane.PlaneX,myPlane.PlaneY,myPlane.PlaneWidth,myPlane.PlaneHeight,enermy.posX,enermy.posY,enermy.width,enermy.height)){
                                            //消灭敌机
                                            enermy.beenHurt(1000);
                                        }
                                    }
                                }
                                else{
                                    if(rectCollisionCheck(myPlane.PlaneX+57,myPlane.PlaneY+37, 10, 10, enermy.posX,enermy.posY,enermy.width,enermy.height)){
                                        var decreaseBlood = Math.min(currentMyHP,enermy.blood);
                                        //敌机少血
                                        enermy.beenHurt(decreaseBlood);
                                        //我方少血
                                        currentMyHP = currentMyHP - decreaseBlood;
                                        isScreenRed = true;
                                        screenRedCount = 10;
                                        //子弹等级降低
                                        if(myPlane.attackType>0){
                                            myPlane.attackType--;
                                        }
                                        //是否死亡
                                        if(currentMyHP <= 0){
                                            currentMyHP = 0;
                                            //死亡
                                            var temp = new ExplosionEffect();
                                            temp.InitExplosionEffect(myPlane.centerPosX, myPlane.centerPosY, 192, 192, imgs_explosion[0],7, 2, 0,false);
                                            explosionsArray.push(temp);

                                            myPlane.visiable = false;
                                            isDead = true;
                                            deadCount = 15;

                                            sfx("explode4");
                                            break againergodicPlanesArray;
                                        }
                                        else{
                                            changeMyPlaneInvincible();
                                            break againergodicPlanesArray;
                                        }
                                    }
                                }
                            }
                        }
                    }
            }
        }

        if(myPlane.visiable){
            //tool和我
            for(var i = 0; i < SkillToolArray.length; i++){
                var tempTool = SkillToolArray[i];
                if(tempTool.visible){
                    if(rectCollisionCheck(tempTool.posX,tempTool.posY,tempTool.width,tempTool.height, myPlane.PlaneX,myPlane.PlaneY,myPlane.PlaneWidth,myPlane.PlaneHeight)){
//                myPlane.catchTool(tempTool.functionType);
                        tempTool.visible = false;
                        if(tempTool.functionType == 0){
                            //提升子弹等级
                            if(myPlane.attackType<AttackTypeMax){
                                myPlane.attackType++;
                            }
                            else{
                                currentMyEX = currentMyEX + 500;
                            }
                        }
                        else if(tempTool.functionType == 1){
                            //防护盾
                            myPlane.creatShield();
                        }
                        else if(tempTool.functionType == 2){
                            //加血
                            currentMyHP = currentMyHP + 40;
                            if(currentMyHP > levelTotalHP){
                                currentMyHP = levelTotalHP;
                            }
                        }
                        sfx("toolcatch");
                    }
                }
            }

            //掉落宝石和我
            for(var i = 0; i < DownDiamendArray.length; i++){
                var tempTool = DownDiamendArray[i];
                if(tempTool.visible){
                    if(rectCollisionCheck(tempTool.posX,tempTool.posY,tempTool.width,tempTool.height, myPlane.PlaneX,myPlane.PlaneY,myPlane.PlaneWidth,myPlane.PlaneHeight)){
                        tempTool.visible = false;
                        //加分
                        currentMyEX = currentMyEX + tempTool.catchScore;
                    }
                    else{
                        if(tempTool.posY < myPlane.PlaneY+myPlane.PlaneHeight){
                            if(myPlane.PlaneY - tempTool.posY <= 250){
                                if(myPlane.centerPosX-20 - tempTool.posX <= 300 && myPlane.centerPosX-20 - tempTool.posX>0){
                                    tempTool.speedX = 10;
                                }
                                else if(tempTool.posX - myPlane.centerPosX-20 <= 300 && tempTool.posX - myPlane.centerPosX-20>0){
                                    tempTool.speedX = -10;
                                }
                                else{
                                    tempTool.speedX = 0;
                                }
                            }
                        }
                        else{
                            tempTool.speedX = 0;
                        }
                    }
                }
            }
        }

    }
}

function changeMyPlaneInvincible(){
    isMyPlaneInvincible = true;
    MyPlaneInvincibleCount = 20;
}

function updateMyPlane(){
    if(myPlane.visiable){
        if(isMoveing){
            myPlane.updatePos(touchX-lastTouchX, touchY-lastTouchY);

            if(lastTouchX != -1){
                if(lastTouchX < touchX){
                    myPlane.direction = 2;
                }
                else if(lastTouchX > touchX){
                    myPlane.direction = 0;
                }
                else{
                    myPlane.direction = 1;
                }
            }
            lastTouchX = touchX;
            lastTouchY = touchY;
            isMoveing = false;
        }
        else{
            myPlane.direction = 1;
        }
        if(effect_count%4 == 0 && !myPlane.hasThunder){
            initBulletArray();
            sfx("shot");
        }
        myPlane.updateAnimRoundPos(effect_count);
        myPlane.updateCenterPos();
        myPlane.updateShieldCount();
        if(myPlane.updateThunderCount()){
            ThunderOver();
        }
    }
}

function drawCoinAndDiamond(){
//    if(fromLeft){
//        canvas.drawImage(img_coin_pre, tempStartX, 19);
//        tempStartX = tempStartX + 4 + 24;
//        drawNum(canvas, myCoin, tempStartX , 22, 15, 16, img_num_money);
////        tempStartX = tempStartX + 4 + checkNumLength( myCoin, 15);
//        tempStartX = tempStartX + 4 + 15*9;
//        canvas.drawImage(img_coin_add, tempStartX, 12);
//        tempStartX = tempStartX + 20 + 32;
//        canvas.drawImage(img_diamond_pre, tempStartX, 17);
//        tempStartX = tempStartX + 4 + 29;
//        drawNum(canvas, myDiamond, tempStartX , 22, 15, 16, img_num_money);
////        tempStartX = tempStartX + 4 + checkNumLength( myDiamond, 15);
//        tempStartX = tempStartX + 4 + 15*9;
//        canvas.drawImage(img_diamond_add, tempStartX, 12);
//        }
//    else{

    var tempStartX = DiamondBtnStartX;
    canvas.drawImage(img_diamond_add, tempStartX, 12);
    tempStartX = tempStartX - 4 - checkNumLength( myDiamond, 15);
//        tempStartX = tempStartX - 4 - 15*9;
    drawNum(canvas, myDiamond, tempStartX , 22, 15, 16, img_num_money);
    tempStartX = tempStartX - 4 - 29;
    canvas.drawImage(img_diamond_pre, tempStartX, 17);
//        tempStartX = tempStartX - 20 - 32;
    tempStartX = CoinBtnStartX;
    canvas.drawImage(img_coin_add, tempStartX, 12);
    tempStartX = tempStartX - 4 - checkNumLength( myCoin, 15);
//        tempStartX = tempStartX - 4 - 15*9;
    drawNum(canvas, myCoin, tempStartX , 22, 15, 16, img_num_money);
    tempStartX = tempStartX - 4 - 24;
    canvas.drawImage(img_coin_pre, tempStartX, 19);
    //}
}

function drawEnermys(){
    for(var i = 0; i < planesArray.length; i++){
        var tempArray = planesArray[i];
        for(var j = 0; j < tempArray.length; j++){
            var enermy = tempArray[j];
            if(enermy.usable){
                enermy.draw(canvas);
            }
        }
    }
}

function drawExplosions(){
    for(var i = 0; i < explosionsArray.length; i++){
        var temp = explosionsArray[i];
        temp.paint(canvas);
    }
}

function drawTools(){
    for(var i = 0; i < SkillToolArray.length; i++){
        var temp = SkillToolArray[i];
        temp.paint(canvas,effect_count);
    }
}

function drawDownDiamond(){
    for(var i = 0; i < DownDiamendArray.length; i++){
        var temp = DownDiamendArray[i];
        temp.paint(canvas);
    }
}

function drawBullets(){
    for(var i = 0; i < bulletArray.length; i++){
        var temp = bulletArray[i];
        temp.paint(canvas);
    }
}

function drawMyPlane(){
    if(isMyPlaneInvincible){
        if(effect_count%2==0){
            myPlane.paint(canvas,effect_count);
        }
    }
    else
        myPlane.paint(canvas,effect_count);
}

function drawMyBulletsArray(){
    for(var i = 0; i < myBulletsArray.length; i++){
//        var tempArray = myBulletsArray[i];
//        for(var j = 0; j < tempArray.length; j++){
//            var temp = tempArray[j];
//            temp.paint(canvas);
//        }
          var temp = myBulletsArray[i];
          temp.paint(canvas);
    }
}

function initOneBullet(bulletspeedX, bulletspeedY, produceX, produceY, bulletType, attack){
    var size = bulletArray.length;
    var temp;
    var needCreatNew = true;

    for(var i = 0; i< size; i++){
        temp = bulletArray[i];
        if(!temp.visible){
            needCreatNew = false;
            break;
        }
    }
    if(needCreatNew){
        temp = new Bullet_Ball();
    }
    var width;
    var height;
    var img;
    switch(bulletType){
        case 0:
            width = 24;
            height = 24;
            img = imgs_bullet[0];
            break;

        case 1:
            width = 24;
            height = 24;
            img = imgs_bullet[1];
            break;

        case 2:
            width = 90;
            height = 174;
            img = imgs_bullet[2];
            break;

        case 3:
            width = 15;
            height = 214;
            img = imgs_bullet[3];
            break;
    }
    temp.InitBullet(width, height, SW, SH, bulletspeedX, bulletspeedY, produceX-width/2, produceY-height/2, img , attack);
    if(needCreatNew){
        bulletArray.push(temp);
    }
}

function initBulletArray(){
    var myBulletWidth;
    var myBulletHeight;
    var myBulletImgType;
    var myBulletAttack;
    var tempType;
    if(myPlane.attackType<=3){
        myBulletWidth = 10;
        myBulletHeight = 24;
        myBulletImgType = 0;
        myBulletAttack = levelBaseBulletAttack;
        tempType = myPlane.attackType;
    }
    else if(myPlane.attackType == 4){
        myBulletWidth = 12;
        myBulletHeight = 26;
        myBulletImgType = 1;
        myBulletAttack = levelBaseBulletAttack+2;
        tempType = 3;
    }
    else if(myPlane.attackType == 5){
        myBulletWidth = 14;
        myBulletHeight = 44;
        myBulletImgType = 2;
        myBulletAttack = levelBaseBulletAttack+4;
        tempType = 3;
    }
    else if(myPlane.attackType == 6){
        myBulletWidth = 16;
        myBulletHeight = 82;
        myBulletImgType = 3;
        myBulletAttack = levelBaseBulletAttack+6;
        tempType = 3;
    }
    else if(myPlane.attackType == 7){
        myBulletWidth = 20;
        myBulletHeight = 120;
        myBulletImgType = 4;
        myBulletAttack = levelBaseBulletAttack+8;
        tempType = 3;
    }
    else if(myPlane.attackType == 8){
        myBulletWidth = 20;
        myBulletHeight = 120;
        myBulletImgType = 5;
        myBulletAttack = levelBaseBulletAttack+10;
        tempType = 3;
    }
    else if(myPlane.attackType == 9){
        myBulletWidth = 20;
        myBulletHeight = 120;
        myBulletImgType = 6;
        myBulletAttack = levelBaseBulletAttack+12;
        tempType = 3;
    }
//    var tempArray = new Array();
    var tempBullet;
    switch(tempType){
        case 3:
            for(var i = 0; i < 2; i++){
                tempBullet = new Bullet_Ball();
                tempBullet.InitBullet(myBulletWidth, myBulletHeight, SW, SH, 0, -35, myPlane.PlaneX+25+73*i-myBulletWidth/2, myPlane.PlaneY+31-myBulletHeight, imgs_myBullet[myBulletImgType], myBulletAttack);
//                tempArray.push(tempBullet);
                myBulletsArray.push(tempBullet);
            }

        case 2:
            for(var i = 0; i < 2; i++){
                tempBullet = new Bullet_Ball();
                tempBullet.InitBullet(myBulletWidth, myBulletHeight, SW, SH, 0, -35, myPlane.PlaneX+34+56*i-myBulletWidth/2, myPlane.PlaneY+26-myBulletHeight, imgs_myBullet[myBulletImgType], myBulletAttack);
//                tempArray.push(tempBullet);
                myBulletsArray.push(tempBullet);
            }

        case 1:
            tempBullet = new Bullet_Ball();
            tempBullet.InitBullet(myBulletWidth, myBulletHeight, SW, SH, 0, -35, myPlane.PlaneX+62-myBulletWidth/2, myPlane.PlaneY-myBulletHeight, imgs_myBullet[myBulletImgType], myBulletAttack);
//            tempArray.push(tempBullet);
            myBulletsArray.push(tempBullet);

        case 0:
            for(var i = 0; i < 2; i++){
                tempBullet = new Bullet_Ball();
                tempBullet.InitBullet(myBulletWidth, myBulletHeight, SW, SH, 0, -35, myPlane.PlaneX+49+26*i-myBulletWidth/2, myPlane.PlaneY+21-myBulletHeight, imgs_myBullet[myBulletImgType], myBulletAttack);
//                tempArray.push(tempBullet);
                myBulletsArray.push(tempBullet);
            }
    }
//    myBulletsArray.push(tempArray);
    //宠物子弹
    var tempUseAnimIndex = 0;
    for(var i = 0; i < animLevelArray.length; i++){
        if(animLevelArray[i] > 0){
            switch(i){
                case 0:
                    myBulletWidth = 20;
                    myBulletHeight = 19;
                    break;

                case 1:
                    myBulletWidth = 20;
                    myBulletHeight = 20;
                    break;

                case 2:
                    myBulletWidth = 20;
                    myBulletHeight = 71;
                    break;

                case 3:
                    myBulletWidth = 20;
                    myBulletHeight = 24;
                    break;

                case 4:
                    myBulletWidth = 20;
                    myBulletHeight = 23;
                    break;

                case 5:
                    myBulletWidth = 20;
                    myBulletHeight = 38;
                    break;
            }
            tempBullet = new Bullet_Ball();
            var tempAnimRoundPos = myPlane.getAnimRoundPosition(tempUseAnimIndex);
            tempBullet.InitBullet(myBulletWidth, myBulletHeight, SW, SH, 0, -35, myPlane.centerPosX+circleRoundXYArray[tempAnimRoundPos][0] - myBulletWidth/2, myPlane.centerPosY+circleRoundXYArray[tempAnimRoundPos][1] - myBulletHeight, imgs_anim_bullet[i], animAttackArray[i]);
            myBulletsArray.push(tempBullet);

            tempUseAnimIndex++;
        }
    }
}

function initOneArrial(){
    var tempArray = new Array();
    switch(arrivalIndex%20){
        case 1:
            var moveType = 3;
            var enermyFlyType = randomNextInt(0,5);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 3;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW-totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, -enermyheight, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], 0, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 2:
            var moveType = 3;
            var enermyFlyType = randomNextInt(0,5);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 4;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX1 = SW/2-totalLength*2 >> 1;
            var startX2 = SW/2+(SW/2-totalLength*2 >> 1);
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposX, tempposY;
                if(i == 0){
                    tempposX = startX1+padding;
                    tempposY = -enermyheight;
                }
                else if(i == 1){
                    tempposX = startX1+totalLength+padding;
                    tempposY = -enermyheight;
                }
                else if(i == 2){
                    tempposX = startX2+padding;
                    tempposY = -enermyheight*2-20;
                }
                else if(i == 3){
                    tempposX = startX2+totalLength+padding;
                    tempposY = -enermyheight*2-20;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], tempposX, tempposY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], 0, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 3:
            var moveType = 0;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 3;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposY;
                if(i==0){
                    tempposY = -enermyheight*2-20;
                }
                else if(i == 1){
                    tempposY =  -enermyheight;
                }
                else if(i == 2){
                    tempposY = -enermyheight*2-20;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempposY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], SH/5, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 4:
            var moveType = 1;
            var enermyFlyType = randomNextInt(0,5);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 4;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW-totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, -enermyheight, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], SH/5, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 5:
            var moveType = 3;
            var enermyFlyType = randomNextInt(0,4);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 5;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW-totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposY;
                if(i == 0){
                    tempposY =  -enermyheight*3;
                }
                else if(i == 1){
                    tempposY =  -enermyheight*2;
                }
                else if(i == 2){
                    tempposY = -enermyheight;
                }
                else if(i == 3){
                    tempposY = -enermyheight*2;
                }
                else if(i == 4){
                    tempposY = -enermyheight*3;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempposY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], 0, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 6:
            var moveType = 2;
            var enermyFlyType = randomNextInt(3,5);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 4;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW-totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, -enermyheight, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], SH/5, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 7:
            var moveType = 0;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 4;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX1 = SW/2-totalLength*2 >> 1;
            var startX2 = SW/2+(SW/2-totalLength*2 >> 1);
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposX;
                if(i == 0){
                    tempposX = startX1+padding;
                }
                else if(i == 1){
                    tempposX = startX1+totalLength+padding;
                }
                else if(i == 2){
                    tempposX = startX2+padding;
                }
                else if(i == 3){
                    tempposX = startX2+totalLength+padding;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], tempposX, -enermyheight, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], SH/4, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 8:
            var moveType = 3;
            var enermyFlyType = randomNextInt(0,4);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 6;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW-totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposY;
                if(i == 0){
                    tempposY =  -enermyheight;
                }
                else if(i == 1){
                    tempposY =  -enermyheight*2;
                }
                else if(i == 2){
                    tempposY = -enermyheight*3;
                }
                else if(i == 3){
                    tempposY = -enermyheight*5;
                }
                else if(i == 4){
                    tempposY = -enermyheight*4;
                }
                else if(i == 5){
                    tempposY = -enermyheight*3;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempposY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], 0, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 9:
            var moveType = 0;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 5;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*3 >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposX, tempposY;
                if(i==0){
                    tempposX = startX+padding;
                    tempposY = -enermyheight*2-10;
                }
                else if(i == 1){
                    tempposX = startX+totalLength+padding;
                    tempposY =  -enermyheight*3-20;
                }
                else if(i == 2){
                    tempposX = startX+totalLength+padding;
                    tempposY = -enermyheight*2-10;
                }
                else if(i == 3){
                    tempposX = startX+totalLength+padding;
                    tempposY = -enermyheight;
                }
                else if(i == 4){
                    tempposX = startX+totalLength*2+padding;
                    tempposY = -enermyheight*2-10;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], tempposX, tempposY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], SH/4, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 10:
            var moveType = 1;
            var enermyFlyType = randomNextInt(0,4);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 5;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempPY;
                var temptargetY;
                switch(i){
                    case 0:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                    case 1:
                        tempPY = -enermyheight*2-20;
                        temptargetY = SH/5-enermyheight-20;
                        break;

                    case 2:
                        tempPY =  -enermyheight;
                        temptargetY = SH/5;
                        break;

                    case 3:
                        tempPY = -enermyheight*2-20;
                        temptargetY = SH/5-enermyheight-20;
                        break;

                    case 4:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                }
                enermyFly_1.initPlane(enermywidth,enermyheight,SW,SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempPY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], temptargetY, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 11:
            var moveType = 3;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 6;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW-totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, -enermyheight, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], 0, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 12:
            var moveType = 1;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 6;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*5 >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempPX,tempPY,temptargetY;
                switch(i){
                    case 0:
                        tempPX = startX+padding;
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                    case 1:
                        tempPX = startX+totalLength+padding;
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5-enermyheight-10;
                        break;

                    case 2:
                        tempPX = startX+totalLength*2+padding;
                        tempPY =  -enermyheight*3-20;
                        temptargetY = SH/5-enermyheight*2-20;
                        break;

                    case 3:
                        tempPX = startX+totalLength*2+padding;
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;

                    case 4:
                        tempPX = startX+totalLength*3+padding;
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5-enermyheight-10;
                        break;

                    case 5:
                        tempPX = startX+totalLength*4+padding;
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                }
                enermyFly_1.initPlane(enermywidth,enermyheight,SW,SH, 0, difficultyArray[1], tempPX, tempPY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], temptargetY, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 13:
            var moveType = 2;
            var enermyFlyType = randomNextInt(0,4);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 5;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempPY;
                var temptargetY;
                switch(i){
                    case 0:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                    case 1:
                        tempPY = -enermyheight*2-20;
                        temptargetY = SH/5-enermyheight-20;
                        break;

                    case 2:
                        tempPY =  -enermyheight;
                        temptargetY = SH/5;
                        break;

                    case 3:
                        tempPY = -enermyheight*2-20;
                        temptargetY = SH/5-enermyheight-20;
                        break;

                    case 4:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                }
                enermyFly_1.initPlane(enermywidth,enermyheight,SW,SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempPY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], temptargetY, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 14:
            var moveType = 3;
            var enermyFlyType = randomNextInt(0,4);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 5;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW-totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposY;
                if(i == 0){
                    tempposY =  -enermyheight;
                }
                else if(i == 1){
                    tempposY =  -enermyheight*2;
                }
                else if(i == 2){
                    tempposY = -enermyheight*3;
                }
                else if(i == 3){
                    tempposY = -enermyheight*2;
                }
                else if(i == 4){
                    tempposY = -enermyheight;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempposY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], 0, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 15:
            var moveType = 0;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 5;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempposY;
                if(i==0){
                    tempposY = -enermyheight*3-20;
                }
                else if(i == 1){
                    tempposY =  -enermyheight*2-10;
                }
                else if(i == 2){
                    tempposY = -enermyheight;
                }
                else if(i == 3){
                    tempposY = -enermyheight*2-10;
                }
                else if(i == 4){
                    tempposY = -enermyheight*3-20;
                }
                enermyFly_1.initPlane(enermywidth, enermyheight, SW, SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempposY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], SH/4, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 16:
            var moveType = 1;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 6;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempPY;
                var temptargetY;
                switch(i){
                    case 0:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                    case 1:
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5-enermyheight-10;
                        break;

                    case 2:
                        tempPY =  -enermyheight*3-20;
                        temptargetY = SH/5-enermyheight*2-20;
                        break;

                    case 3:
                        tempPY =  -enermyheight*3-20;
                        temptargetY = SH/5-enermyheight*2-20;
                        break;

                    case 4:
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5-enermyheight-10;
                        break;

                    case 5:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                }
                enermyFly_1.initPlane(enermywidth,enermyheight,SW,SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempPY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], temptargetY, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 17:
            var moveType = 2;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 6;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*5 >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempPX,tempPY;
                var temptargetY;
                switch(i){
                    case 0:
                        tempPX = startX+padding;
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5 -enermyheight-10;
                        break;
                    case 1:
                        tempPX = startX+totalLength+padding;
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5 -enermyheight-10;
                        break;

                    case 2:
                        tempPX = startX+totalLength*2+padding;
                        tempPY =  -enermyheight*3-20;
                        temptargetY = SH/5 -enermyheight*2-20;
                        break;

                    case 3:
                        tempPX = startX+totalLength*2+padding;
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;

                    case 4:
                        tempPX = startX+totalLength*3+padding;
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5 -enermyheight-10;
                        break;

                    case 5:
                        tempPX = startX+totalLength*4+padding;
                        tempPY = -enermyheight*2-10;
                        temptargetY = SH/5 -enermyheight-10;
                        break;
                }
                enermyFly_1.initPlane(enermywidth,enermyheight,SW,SH, 0, difficultyArray[1], tempPX, tempPY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], temptargetY, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 18:
            var moveType = 0;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 6;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                enermyFly_1.initPlane(enermywidth,enermyheight,SW,SH, 0, difficultyArray[1], startX+totalLength*i+padding, -(2+i)/2*enermyheight, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], SH/4, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 19:
            var moveType = 1;
            var enermyFlyType = randomNextInt(0,3);
            var difficultyArray = gainDifficulty(moveType,enermyFlyType);
            var planesSum = 6;
            var enermywidth = getEnermyPlanesWidthHeight(enermyFlyType,true);
            var enermyheight = getEnermyPlanesWidthHeight(enermyFlyType,false);
            var padding = 10;
            var totalLength = padding*2+enermywidth;
            var startX = SW - totalLength*planesSum >> 1;
            for(var i = 0; i < planesSum; i++){
                var enermyFly_1 = new EnermyFly_1();
                var tempPY;
                var temptargetY;
                switch(i){
                    case 0:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                    case 1:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;

                    case 2:
                        tempPY =  -enermyheight*2-20;
                        temptargetY = SH/5-enermyheight-20;
                        break;

                    case 3:
                        tempPY =  -enermyheight*2-20;
                        temptargetY = SH/5-enermyheight-20;
                        break;
                    case 4:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;

                    case 5:
                        tempPY = -enermyheight;
                        temptargetY = SH/5;
                        break;
                }
                enermyFly_1.initPlane(enermywidth,enermyheight,SW,SH, 0, difficultyArray[1], startX+totalLength*i+padding, tempPY, imgs_enermyFly1[enermyFlyType*2], imgs_enermyFly1[enermyFlyType*2+1], difficultyArray[0], temptargetY, moveType, getEnermyFightType(moveType), difficultyArray[2], difficultyArray[3], difficultyArray[4], difficultyArray[5]);
                tempArray.push(enermyFly_1);
            }
            break;

        case 0:
            var level = zhengchu(arrivalIndex-1, 20);
            var bossType = level%(bossLevelArray.length);
            bossLevelArray[bossType] = bossLevelArray[bossType] + 1;

            var bossWidth;
            var bossHeight;
            var bossBlood;
            var baseBulletSpeed, bulletAttack, bossScore;
            switch(bossType){
                case 0:
                    bossWidth = 300;
                    bossHeight = 223;
                    bossBlood = 5000+bossLevelArray[bossType]*2000;
                    baseBulletSpeed = 10+bossLevelArray[bossType];
                    bulletAttack = 10+bossLevelArray[bossType]*5;
                    bossScore = 2000+1000*bossLevelArray[bossType];
                    break;

                case 1:
                    bossWidth = 297;
                    bossHeight = 194;
                    bossBlood = 7000+bossLevelArray[bossType]*2000;
                    baseBulletSpeed = 10+bossLevelArray[bossType];
                    bulletAttack = 12+bossLevelArray[bossType]*5;
                    bossScore = 3000+1000*bossLevelArray[bossType];
                    break;

                case 2:
                    bossWidth = 300;
                    bossHeight = 224;
                    bossBlood = 8000+bossLevelArray[bossType]*2000;
                    baseBulletSpeed = 10+bossLevelArray[bossType];
                    bulletAttack = 14+bossLevelArray[bossType]*5;
                    bossScore = 4000+1000*bossLevelArray[bossType];
                    break;

                case 3:
                    bossWidth = 296;
                    bossHeight = 213;
                    bossBlood = 8000+bossLevelArray[bossType]*2000;
                    baseBulletSpeed = 10+bossLevelArray[bossType];
                    bulletAttack = 15+bossLevelArray[bossType]*5;
                    bossScore = 5000+1000*bossLevelArray[bossType];
                    break;
            }
            var enermyFly_Boss1 = new EnermyFly_Boss1();
            enermyFly_Boss1.initPlane(bossWidth, bossHeight, SW, SH, 0, 10, SW-bossWidth>>1, -bossHeight, imgs_boss[bossType*2], imgs_boss[bossType*2+1], bossBlood, 50, baseBulletSpeed, bulletAttack, bossType, bossLevelArray[bossType], bossScore);
            tempArray.push(enermyFly_Boss1);
            isBossComing = true;
            //警告
            warningObject.InitEffect(225, 148, 0, -20, SW/2, SH/3, img_warning, 20,20);
            //取消飞机无敌
            myPlane.hasThunder = false;
            ThunderOver();
            break;

    }
    planesArray.push(tempArray);
}

function defeatOneEnermy(centerX, centerY, isBoss, defeatScore){
    var temp = new ExplosionEffect();
    temp.InitExplosionEffect(centerX, centerY, 192, 192, imgs_explosion[1],7, 1, 0,false);
    explosionsArray.push(temp);

    if(isBoss){
        temp = new ExplosionEffect();
        temp.InitExplosionEffect(centerX-50, centerY-50, 192, 192, imgs_explosion[1],7, 1,4,false);
        explosionsArray.push(temp);

        temp = new ExplosionEffect();
        temp.InitExplosionEffect(centerX-50, centerY+50, 192, 192, imgs_explosion[1],7, 1,8,false);
        explosionsArray.push(temp);

        temp = new ExplosionEffect();
        temp.InitExplosionEffect(centerX+50, centerY, 192, 192, imgs_explosion[1],7, 1,12,false);
        explosionsArray.push(temp);

        if(!isGameHasArrivalLimit){
            isBossComing = false;
            changingMap = true;
            changingAlphaCount = 0;
            isChangingAlphaCountIncrease = true;
        }
        sfx("explode4");
    }
    else{
        sfx("explode6");
    }

    currentMyEX = currentMyEX + defeatScore;
    //宝石掉落
    if(!isStartFromBlackStage){
        var diamondSum;
        var diamondW = 35;
        var diamondH = 43;
        var diamondScore = 30;
        if(isBoss){
            diamondSum = zhengchu(zhengchu(defeatScore,10),diamondScore);
        }
        else{
            diamondSum = zhengchu(zhengchu(defeatScore,2),diamondScore);
        }
        var startX,startY;
        if(diamondSum <= 10){
            startX = centerX - diamondSum*diamondW/2;
            for(var i = 0; i < diamondSum; i++){
                var downDiamond = new Down_Diamend();
                startY = centerY+randomNextInt(0,diamondH);
                downDiamond.InitEffect(diamondW, diamondH, SW, SH, 0, -10, startX + i*diamondW, startY, imgs_diamend[randomNextInt(0,1)], diamondScore);
                DownDiamendArray.push(downDiamond);
            }
        }
        else{
            if(diamondSum>=20){
                diamondSum = 20;
            }
            var startX1,startX2;
            startX1 = centerX - 10*diamondW/2;
            startX2 = (diamondSum-10)*diamondW/2;
            for(var i = 0; i < diamondSum; i++){
                if(i >= 10){
                    startX = startX2 + (i-10)*diamondW;
                    startY = centerY-diamondH+randomNextInt(0,diamondH);
                }
                else{
                    startX = startX1 + i*diamondW;
                    startY = centerY+randomNextInt(0,diamondH);
                }
                var downDiamond = new Down_Diamend();
                downDiamond.InitEffect(diamondW, diamondH, SW, SH, 0, -10, startX, startY, imgs_diamend[randomNextInt(0,1)], diamondScore);
                DownDiamendArray.push(downDiamond);
            }
        }
    }

//    if(currentMyEX >= levelTotalEX){
//        myPlaneLevelUp();
//        levelupObject.InitEffect(180, 40, 0, -8, myPlane.PlaneX+myPlane.PlaneWidth/2, myPlane.PlaneY, img_levelup, 20);
//    }

    defeatEnermyCount++;
    if(defeatEnermyCount % 15 ==0){
        var tempToolIndex = randomNextInt(1,4);
        creatOneTool(tempToolIndex, centerX, centerY);
    }
}

//增加难度
function gainDifficulty(moveType,enermyFlyType){
    var level = zhengchu(arrivalIndex-1, 20);
    var enermyblood,enermyspeedY,fightTotal, baseBulletSpeed, bulletAttack, defeatScore;
    switch(moveType){
        case 0:
            //到达位置后加速
            enermyblood = 20+enermyFlyType*10+level*40;
            enermyspeedY = 8 + level;
            baseBulletSpeed = 4+level*2;
            fightTotal = 30-5*level
            if(fightTotal<5){
                fightTotal = 5;
            }
            bulletAttack = 5+level*2;
            defeatScore = 100+enermyFlyType*10+level*20;
            break;

        case 1:
            //到达位置后原地不动
            enermyblood = 30+enermyFlyType*10+level*40;
            enermyspeedY = 10;
            baseBulletSpeed = 10+level*2;
            fightTotal = 60-10*level
            if(fightTotal<10){
                fightTotal = 10;
            }
            bulletAttack = 8+level*2;
            defeatScore = 60+enermyFlyType*10+level*20;
            break;

        case 2:
            //到达位置后往x轴消失
            enermyblood = 20+enermyFlyType*10+level*40;
            enermyspeedY = 8 + level;
            baseBulletSpeed = 12+level*2;
            fightTotal = 60-10*level
            if(fightTotal<10){
                fightTotal = 10;
            }
            bulletAttack = 7+level*2;
            defeatScore = 70+enermyFlyType*10+level*20;
            break;

        case 3:
            //到达位置后不变化
            enermyblood = 30+enermyFlyType*10+level*40;
            enermyspeedY = 8 + level;
            baseBulletSpeed = 10+level*2;
            fightTotal = 60-10*level
            if(fightTotal<10){
                fightTotal = 10;
            }
            bulletAttack = 6+level*2;
            defeatScore = 80+enermyFlyType*10+level*20;
            break;
    }
    var tempArray = new Array();
    tempArray.push(enermyblood);
    tempArray.push(enermyspeedY);
    tempArray.push(fightTotal);
    tempArray.push(baseBulletSpeed);
    tempArray.push(bulletAttack);
    tempArray.push(defeatScore);
    return tempArray;
}

function getEnermyFightType(moveType){
    var level = zhengchu(arrivalIndex-1, 20);
    switch(moveType){
        case 0:
            //到达位置后加速
            if(level == 0){
                var randomtemp = randomNextInt(0,2);
                if(randomtemp == 0){
                    return 0;
                }
                else if(randomtemp == 1){
                    return 1;
                }
                else if(randomtemp == 2){
                    return 7;
                }
            }
            else if(level < 4){
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return level;
                }
                else if(randomtemp == 1){
                    return level+6;
                }
            }
            else{
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return 3;
                }
                else if(randomtemp == 1){
                    return 9;
                }
            }
            break;

        case 1:
            //到达位置后原地不动
            if(level < 3){
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return level+1;
                }
                else if(randomtemp == 1){
                    return level+4;
                }
            }
            else{
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return 3;
                }
                else if(randomtemp == 1){
                    return 6;
                }
            }
            break;

        case 2:
            //到达位置后往x轴消失
            if(level < 4){
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return level+1;
                }
                else if(randomtemp == 1){
                    return level+4;
                }
            }
            else{
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return 3;
                }
                else if(randomtemp == 1){
                    return 6;
                }
            }
            break;

        case 3:
            //到达位置后不变化
            if(level == 0){
                var randomtemp = randomNextInt(0,2);
                if(randomtemp == 0){
                    return 0;
                }
                else if(randomtemp == 1){
                    return 1;
                }
                else if(randomtemp == 2){
                    return 4;
                }
            }
            else if(level < 4){
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return level;
                }
                else if(randomtemp == 1){
                    return level+3;
                }
            }
            else{
                var randomtemp = randomNextInt(0,1);
                if(randomtemp == 0){
                    return 3;
                }
                else if(randomtemp == 1){
                    return 6;
                }
            }
            break;
    }
}

function getEnermyPlanesWidthHeight(planeType,isGetWidth){
    if(isGetWidth){
        switch(planeType){
            case 0:
                return 45;

            case 1:
                return 39;

            case 2:
                return 51;

            case 3:
                return 60;

            case 4:
                return 70;

            case 5:
                return 97;
        }
    }
    else{
        switch(planeType){
            case 0:
                return 40;

            case 1:
                return 45;

            case 2:
                return 53;

            case 3:
                return 60;

            case 4:
                return 54;

            case 5:
                return 78;
        }
    }
}

function myPlaneLevelUp(){
    currentMyHP = levelTotalHP = 60+30*currentMyLevel;
//    levelTotalEX = levelTotalEX+currentMyLevel*4000+10000;
    levelBaseBulletAttack = 6+currentMyLevel;
    currentMyLevel++;
//    if(currentMyLevel==1){
//        myPlane.changePlane(imgs_myPlane[0],18,SW);
//    }
//    else if(currentMyLevel==2){
        myPlane.changePlane(imgs_myPlane[fightDriverIndex],12,SW);
//    }
//    else if(currentMyLevel==3){
//        myPlane.changePlane(imgs_myPlane[2],0,SW);
//    }
}

function ThunderOver(){
    movement = 4;
    //送护盾
    myPlane.creatShield();
}

function creatThunder(){
    myPlane.creatThunder();
    movement = 30;
}

function creatBomb(){
    isKickBomb = true;
    KickBombCount = 10;

    sfx("explode5");
    //释放爆炸效果
    var temp = new ExplosionEffect();
    temp.InitExplosionEffect(128, 576+96, 192, 192, imgs_explosion[0],7, 1, 0,false);
    explosionsArray.push(temp);

    temp = new ExplosionEffect();
    temp.InitExplosionEffect(384, 384+96, 192, 192, imgs_explosion[0],7, 1, 2,false);
    explosionsArray.push(temp);

    temp = new ExplosionEffect();
    temp.InitExplosionEffect(128, 192+96, 192, 192, imgs_explosion[0],7, 1, 4,false);
    explosionsArray.push(temp);

    temp = new ExplosionEffect();
    temp.InitExplosionEffect(384, 0+96, 192, 192, imgs_explosion[0],7, 1, 6,false);
    explosionsArray.push(temp);

    temp = new ExplosionEffect();
    temp.InitExplosionEffect(384, 576+96, 192, 192, imgs_explosion[0],7, 1, 3,false);
    explosionsArray.push(temp);

    temp = new ExplosionEffect();
    temp.InitExplosionEffect(128, 384+96, 192, 192, imgs_explosion[0],7, 1, 6,false);
    explosionsArray.push(temp);

    temp = new ExplosionEffect();
    temp.InitExplosionEffect(384, 192+96, 192, 192, imgs_explosion[0],7, 1, 9,false);
    explosionsArray.push(temp);

    temp = new ExplosionEffect();
    temp.InitExplosionEffect(128, 0+96, 192, 192, imgs_explosion[0],7, 1, 12,false);
    explosionsArray.push(temp);
}

function creatOneTool(tempToolIndex, centerX, centerY){
    var toolWidth,toolHeight,toolImgIndex,toolFunctionType;
    if(tempToolIndex<4){
        if(currentMyHP < levelTotalHP && tempToolIndex == 3){
            toolWidth = 50;
            toolHeight = 48;
            toolImgIndex = 2;
            toolFunctionType = 2;
        }
        else{
            toolWidth = 48;
            toolHeight = 48;
            toolImgIndex = 0;
            toolFunctionType = 0;
        }
    }
    else{
        toolWidth = 50;
        toolHeight = 50;
        toolImgIndex = 1;
        toolFunctionType = 1;
    }

    var temp = new SkillTool();
    var direction = 1;
    if(randomNextInt(0,1) == 1){
        direction = -1;
    }
    temp.InitSkillTool(toolWidth, toolHeight, SW, SH, direction*randomNextInt(0,5), 5, centerX, centerY, imgs_tool[toolImgIndex], toolFunctionType);
    SkillToolArray.push(temp);
}

function setCircleRoundXYArray(load_banjing){
    var tempXYArray;
    tempXYArray = [0, -load_banjing];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.25, -load_banjing*0.96];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.5, -load_banjing*0.86];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.7, -load_banjing*0.7];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.86, -load_banjing*0.5];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.96, -load_banjing*0.25];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing, 0];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.96, load_banjing*0.25];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.86, load_banjing*0.5];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.7, load_banjing*0.7];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.5, load_banjing*0.86];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [-load_banjing*0.25, load_banjing*0.96];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [0, load_banjing];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.25, load_banjing*0.96];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.5, load_banjing*0.86];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.7, load_banjing*0.7];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.86, load_banjing*0.5];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.96, load_banjing*0.25];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing, 0];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.96, -load_banjing*0.25];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.86, -load_banjing*0.5];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.7, -load_banjing*0.7];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.5, -load_banjing*0.86];
    circleRoundXYArray.push(tempXYArray);
    tempXYArray = [load_banjing*0.25, -load_banjing*0.96];
    circleRoundXYArray.push(tempXYArray);
}

function createDriverNames(){
    DriverNameArray = [
        ["艾琳","艾娜","那维亚","希拉瑞莉","爱莉丝","艾尔玛","伊莎蓓尔","艾薇","婕咪","珍","珍尼丝","约瑟芬","朱蒂","朱莉娅","凯瑟琳","莉娜","莉萨","琳","玛琪","曼蒂"],
        ["路易斯","卢卡","卢卡斯","迪特玛","费恩","约纳斯","费力克斯","雷奥","保罗","马克西米连","尤利安","尼科拉斯","马科斯","本雅明","以利亚","雅各布","菲利普","诺亚","雅尼克","大卫"],
        ["安妮","汉娜","玛丽亚","海伦娜","莫妮卡","雷娅","艾米丽","拉里萨","爱玛","萨拉","劳拉","伊丽莎白","丽娜","马利亚","苏菲","奈利亚","约翰娜","索非娅","丽莎","玛雅"],
        ["李大嘴","丁聪华","曾帛员","韩 松","江浩华","田宇旺","孔良超","冉迪振","崔子希","陈寿渊","徐经岚","任 希","姚道益","米泽升","陆银兴","康天鹏","顾良龙","许永岚","寇瑞生","黄敬甫"]
    ];
}

function destoryDriverNames(){
    DriverNameArray.splice(0,DriverNameArray.length);
    DriverNameArray = null;
}

function firstGetUserInfo(){
        loading4JS.showLoading("正在读取个人数据，请等待！");
        myAjax(backDomain+"airwarback/getinfo",{userId:yidong_userId},function(res){
            if(res.code == "0"){
                var userObj = res.user;
                if(userObj.newer == 1){
                    isNewPlayer = true;
                }
                else{
                    isNewPlayer = false;
                }

                if(userObj.haslottochance == 1){
                    hasLottoChance = true;
                }
                else{
                    hasLottoChance = false;
                }

                driverIndex = fightDriverIndex = userObj.fight_index;

                myCoin =userObj.coin;
                myDiamond =userObj.diamond;
                myMaxScore =userObj.max_score;

                //驾驶员名字
                createDriverNames();
                var str_name_index =userObj.name_index;
                var str_name_changeable =userObj.name_changeable;
                hasChooseNameArray = str_name_changeable.split(",");
                driverNameIndex = str_name_index.split(",");
                for(var i=0;i<hasChooseNameArray.length;i++){
                    if(hasChooseNameArray[i] == "1"){
                        hasChooseNameArray[i] = false;
                    }
                    else{
                        hasChooseNameArray[i] = true;
                    }
                    driverNameIndex[i] = parseInt(driverNameIndex[i]);
                    if(hasChooseNameArray[i]){
                        driverName[i] = DriverNameArray[i][driverNameIndex[i]];
                    }
                    else{
                        driverName[i] = DriverNameArray[i][0];
                    }
                }

                //战机等级
                var str_plane_level = userObj.plane_level;
                myPlaneLevel = str_plane_level.split(",");
                for(var i=0;i<myPlaneLevel.length;i++){
                    myPlaneLevel[i] = parseInt(myPlaneLevel[i]);
                    myFightAttack[i] = 5000 + (myPlaneLevel[i]-1)*500;
                }

                //宠物等级
                var str_pet_level = userObj.pet_level;
                animLevelArray = str_pet_level.split(",");
                for(var i=0;i<animLevelArray.length;i++){
                    animLevelArray[i] = parseInt(animLevelArray[i]);
                    animAttackArray[i] = i+animLevelArray[i];
                }

                //材料数组
                var str_material_array = userObj.material_array;
                myMaterialArray = str_material_array.split(",");
                for(var i=0;i<myMaterialArray.length;i++){
                    myMaterialArray[i] = parseInt(myMaterialArray[i]);
                }

                //道具数组
                var str_tool_array = userObj.tool_array;
                myToolArray = str_tool_array.split(",");
                for(var i=0;i<myToolArray.length;i++){
                    myToolArray[i] = parseInt(myToolArray[i]);
                }

                //关卡解锁情况
                var str_mission_unlock =userObj.mission_unlock;
                MissionUnlock = str_mission_unlock.split(",");
                for(var i=0;i<MissionUnlock.length;i++){
                    if(MissionUnlock[i] == "1"){
                        MissionUnlock[i] = true;
                    }
                    else{
                        MissionUnlock[i] = false;
                    }
                }
                //黑暗等级
                currentBlackStage = userObj.black_level;
                if(userObj.black_unlock == 1){
                    isBlackStateUnlock = true;
                }
                else{
                    isBlackStateUnlock = false;
                }

                loading4JS.closeLoading();

                changeState(6);
            }
            else{
                loading4JS.closeLoading();
                toast4JS.buildToast("读取失败，请重试！",1500);
            }
        },function(){
            loading4JS.closeLoading();
            toast4JS.buildToast("读取失败，请重试！",1500);
        });
}

//function planeRevive(){
//    restartTimes++;
//    myDiamond = myDiamond - restartCostDiamond;
//
//
//    myPlane.visiable = true;
//    currentMyHP = levelTotalHP;
//    //改变位置
//    myPlane.PlaneX = SW-124>>1;
//    myPlane.PlaneY = 600;
//    //添加护盾
//    myPlane.creatShield();
//    //子弹升级
//    myPlane.attackType = 3;
//    changeState(2);
//    //清理
//    deadScreenShot = null;
//
//    updateMasonry(myDiamond);
//}
